Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';设置状态';未定义的PokeApi_Javascript_Reactjs - Fatal编程技术网

Javascript TypeError:无法读取属性';设置状态';未定义的PokeApi

Javascript TypeError:无法读取属性';设置状态';未定义的PokeApi,javascript,reactjs,Javascript,Reactjs,我正在尝试使用params将文本从输入文本类型发送到另一个组件。但我犯了下一个错误 TypeError:无法读取未定义的属性“setState” 我认为使用重定向是最好的选择,但我不确定 export default class SearchBar extends React.Component { constructor(props) { super(props); this.state = {redirect: false , value: ''}; }

我正在尝试使用params将文本从输入文本类型发送到另一个组件。但我犯了下一个错误

TypeError:无法读取未定义的属性“setState”

我认为使用重定向是最好的选择,但我不确定

export default class SearchBar extends React.Component {
    
  constructor(props) {
    super(props);
    this.state = {redirect: false , value: ''};
  }

    setRedirect(event) {
        this.setState({
          redirect: true,
          value: event.target.value
        });
      };
      renderRedirect = () => {
        if (this.state.redirect) {
          return <Redirect to={{
            pathname: `/pokemon/filtered/${this.state.value}`,
           }}/>;
        }
      };

    render() {
      return (
        <form>
          <input type="text" value={this.state.value} onChange={this.setRedirect()} />
        </form>
      );
    }
  }
导出默认类搜索栏扩展React.Component{
建造师(道具){
超级(道具);
this.state={redirect:false,值:“”};
}
设置重定向(事件){
这是我的国家({
重定向:对,
值:event.target.value
});
};
RenderDirect=()=>{
if(this.state.redirect){
返回;
}
};
render(){
返回(
);
}
}

您必须在此处输入错误

  • onChange
    的输入行上,您实际上调用了函数,因为您添加了“()”
    你应该做:
  • 或者使用箭头函数来简化事情 因为arrow函数将始终属于调用该函数的对象

  • 在react中绑定事件处理程序的正确方法是

  • 在javascript世界中<代码>此默认设置为调用者。请参见MDN上的
    Function.prototype.bind
    。 要将类方法绑定到类,可以1。使用公共类字段语法。2.在构造函数中绑定

  • 参考资料:

     <input type="text" value={this.state.value} onChange={this.setRedirect} />
    
      constructor(props) {
        super(props);
        this.state = {redirect: false , value: ''};
        this.setRedirect = this.bind.setRedirect(this)
      }