的特性「狀態,」我有一個輸入和一個按鈕無法讀取空
<input className="form-control" value={this.state.sentence} onChange={this.onChange}/>
<button type="button" onClick={this.handleSentence}></button>
我在構造函數中綁定兩種功能。
onChange(e) {this.setState({sentence: e.target.value});}
handleSentence(e) {console.log('string -->',this.state.sentence)}
上handleSentence
功能log
回報Cannot read property 'state' of null
。
但render(let{sentence}=this.state)
返回正確的值,也是我看到我輸入
此處鍵入是構造函數:
class SentenceForm extends Component {
constructor(props) {
super(props)
this.state = {
sentence: '',
splitedSentenceArray:[]
}
this.onChange = this.onChange.bind(this);
this.onClick = this.handleSentence.bind(this);
}
這是空的,所以我想你沒有綁定功能組件範圍或你做錯了。粘貼整個組件代碼請 –
@PiotrSołtysiak我做了添加構造函數 –