,我發現了以下錯誤,但一直沒能找出爲什麼它正在發生陣營:元素正在改變類型文本的控制輸入不受控制
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中,並沒有任何警告或錯誤 –