,我发现了以下错误,但一直没能找出为什么它正在发生阵营:元素正在改变类型文本的控制输入不受控制
warning.js:36警告:搜索正在改变受控输入的文字类型将不受控制。输入元素不应该从受控状态切换到非受控状态(反之亦然)。使用受控的或不受控制的输入元件,用于所述部件的寿命之间决定
我已经得到了以下文件:
- Searchbox.js
import React, { Component } from 'react';
class Searchbox extends Component {
// render method
render() {
const { value, onChange, onSubmit, children } = this.props
console.log(value)
console.log(onChange)
console.log(onSubmit)
console.log(children)
return (
<section className="search-section">
<form onSubmit={onSubmit}>
<input
type="text"
className="search-box"
value={value}
onChange={onChange}
/>
<button type="submit">{children}</button>
</form>
</section>
)
}
}
export default Searchbox
和App.js文件
import React, { Component } from 'react';
import Searchbox from './components/Searchbox'
//import logo from './logo.svg';
import './App.css';
const DEFAULT_TERM = 'High Fidelity'
class App extends Component {
// Constructor
constructor(props) {
super(props)
this.state = {
movie: null,
searchTerm: DEFAULT_TERM
}
this.onSearchSubmit = this.onSearchSubmit.bind(this)
this.onSearchChange = this.onSearchChange.bind(this)
}
// onSearchSubmit method
onSearchSubmit(e) {
e.preventDefault()
console.log("Searching movie with name" + this.status.searchTerm)
}
onSearchChange(e){
console.log(event.target.value)
this.setState({ searchTerm: event.target.value })
}
// render method
render() {
const { movie, searchTerm } = this.state
return (
<div className="App">
<Searchbox
value={searchTerm}
onChange={this.onSearchChange}
onSubmit={this.onSearchSubmit}
>Search
</Searchbox>
</div>
);
}
}
export default App;
在负载一切都很好,但是当我在文本字段中键入一些则错误被触发。
有什么建议吗?
它为我工作的罚款。 –
刚刚将你的代码粘贴到一个codepen中,并没有任何警告或错误 –