[英]React Component errors (Input)
[SOLVED] Solution was: 1) the 'extends React.Component' doesn't autoBind any more. 解决方法是:1)'扩展React.Component'不再自动绑定。 2) 'filterText' variable needed to be sended 'handleUserInput(filterText)'...
2)需要发送'filterText'变量'handleUserInput(filterText)'...
Thank you for All!! 感谢你所做的一切!!
Good day. 美好的一天。 This code is working on "React.createClass" and now I'm trying to change it to "extends React.Component".
这段代码正在“ React.createClass”上运行,现在我试图将其更改为“扩展React.Component”。 The aim is to sort the elements by entering value into input.
目的是通过在输入中输入值来对元素进行排序。
The code : 代码 :
TheSecondComponent...
constructor(props){
super(props);
}
handleChange() {
this.props.onUserInput(
this.refs.filterTextInput.value
);
}
render() {
return (
<form className='Center'>
<input
type="text"
value={this.props.filterText}
ref="filterTextInput"
onChange={this.handleChange}
/>
</form>
);
}
TheFirstComponent...
constructor(){
super();
this.state={'filterText': ''} // If to use setState here than I have one
more error, added in the end of the question
this.handleUserInput=this.handleUserInput.bind(this);
}
getInitialState() {
return {
filterText: ''
};
}
handleUserInput(filterText) {
//this.setState({filterText: filterText});
this.state = {filterText: filterText};
}
render() {
return (
<div>
<div>
<SearchBar
filterText={this.state.filterText}
onUserInput={this.handleUserInput}
/>
<CreateButtons
source={this.props.citys}
filterText={this.state.filterText}
/>
</div>
</div>
);
}
The WORKING code of the "React.createClass": “ React.createClass”的工作代码:
TheFirstComponent...
({
handleChange: function() {
this.props.onUserInput(
this.refs.filterTextInput.value
);
},
render: function() {
return (
<form className='Center'>
<input
type="text"
value={this.props.filterText}
ref="filterTextInput"
onChange={this.handleChange}
/>
</form>
);
}
});
TheSecondComponent...
({
getInitialState: function() {
return {
filterText: ''
};
},
handleUserInput: function(filterText) {
this.setState({
filterText: filterText
});
},
render: function() {cl(this);
return (
<div>
<div className='SLFirst'>
<SearchBar
filterText={this.state.filterText}
onUserInput={this.handleUserInput}
/>
<CreateButtons
source={this.props.citys}
filterText={this.state.filterText}
/>
</div>
</div>
);
}
});
Have an error: I am trying to input any text in the "input" but it doesn't write the value. 发生错误:我尝试在“输入”中输入任何文本,但未写入值。 I mean, I am typing, but nothing changes.
我的意思是,我正在打字,但没有任何变化。 is it because of
是因为
this.state={'filterText': ''}
? this.state={'filterText': ''}
吗?
And how should I play with it? 我应该怎么玩呢? If to set not '' but 'sometext' then this 'sometext' will be unchangable.
如果将“ sometext”设置为“ not”,则此“ sometext”将不可更改。
As I found "setState" is no longer working. 正如我发现的“ setState”不再起作用。 And if to use 'setState' then have oooone more:
如果要使用“ setState”,还需要更多:
TypeError: Cannot read property 'filterText' of null
40 | <div>
41 | <div className='SLFirst'>
42 | <SearchBar
> 43 | filterText={this.state.filterText}
44 | onUserInput={this.handleUserInput}
45 | />
46 | <CreateButtons
You didn't bind the handleChange() . 您没有绑定handleChange() 。 To fix this problem you have to bind the methods which are using "this" in constructor().
若要解决此问题,您必须绑定在Constructor()中使用“ this”的方法。 Try the following code
试试下面的代码
constructor(props){
super(props);
this.handleChange = this.handleChange.bind(this)
}
handleChange() {
this.props.onUserInput(
this.refs.filterTextInput.value
);
}
render() {
return (
<form className='Center'>
<input
type="text"
value={this.props.filterText}
ref="filterTextInput"
onChange={this.handleChange}
/>
</form>
);
}
this.state = {...} will not trigger a re-render, to update your state use the setState function this.state = {...}不会触发重新渲染,使用setState函数更新您的状态
setState can take either an object setState可以接受一个对象
//the following will update the filterText property on the state object
this.setState({filterText: ''})
or a function 或功能
this.setState(function(state){
state.filterText = '';
// make sure you return state
return state
})
make sure you initialize your state object in your constructor as follows 确保您在构造函数中初始化状态对象,如下所示
constructor(props){
super(props);
this.state = {
filterText: 'initiale value'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.