React JS:从 React.createClass 更新时无法读取未定义的属性'setState'



我正在更新一个反应组件以使用:

class searchFilms扩展了react.component

而不是:

var SearchFilms = react.CreateClass({

(

当我运行此操作时,我会收到以下错误:"无法读取未定义的属性'setState'

谁能建议我如何更新?

class SearchFilms extends React.Component { 
//var SearchFilms = React.createClass({
    getInitial() {
        return {
            userInput: ''
        };
    }
    onChange(event) {
        this.setState({
            userInput: event.target.value
        });
        this.props.newVal(event.target.value);
    }
    render() {
        var languages = ['Shutter Island', 'Inception', 'Titanic', 'Blood Diamond'];
        return (
            <div className="row" id="Nav">
                <input type="text" 
                    value={this.props.userInput}
                    onChange={this.onChange}
                />
            </div>
        )       
    }
}

onChange中的 thisundefined,这是由不绑定引起的。

添加这些:

constructor(props) {
  super(props);
  this.onChange = this.onChange.bind(this);
}

fyi,

  • https://facebook.github.io/react/docs/handling-events.html
  • 反应是不确定的

最新更新