的特性“状态,”我有一个输入和一个按钮无法读取空
<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我做了添加构造函数 –