简体   繁体   English

SetState不是OnChange上的函数

[英]SetState Is Not A Function on OnChange

Working to get a slider to change the value of "text" in the React state. 正在努力获得一个滑块,以在React状态下更改“文本”的值。

Keep getting an error: 不断出现错误:

"App.js:90 Uncaught TypeError: this.setState is not a function" despite my best troubleshooting efforts. 尽管我尽了最大的故障排除努力,但“ App.js:90 Uncaught TypeError:this.setState不是一个函数”。

What could the fix be? 解决办法是什么?

  class App extends Component {
  constructor(props) {
      super(props);
      this.state = {list: [{x: "Before Pool", y:85000}, {x: "After Pool", y:82000}], text: 0, options: {bathrooms:'', bedrooms:'', sqft:''}};
    }

  componentDidMount() {
        setTimeout(() => {
         this.setState({list: [{x: "Before Pool", y:60000}, {x: "After Pool", y:30000}]});
         console.log("testing", this.state.text);
       }, 2000) ;
  }
  handleChange (event) {
    console.log("from handle change", event);
   this.setState({text : event });
  }
  render() {
    return (
      <div className="App">
          <div>
             <div style={wrapperStyle}>
               <p># of Bathrooms</p>
               <Slider min={0} max={20} defaultValue={3} onChange={this.handleChange} />
             </div>

在此处输入图片说明 在此处输入图片说明

您需要绑定handleChange方法

<Slider min={0} max={20} defaultValue={3} onChange={this.handleChange.bind(this)}

You need to bind your state to the callback within setTimeout since you are in a different context. 由于您处于不同的上下文中,因此需要将状态绑定到setTimeout的回调。 I belive this would do the trick: 我相信这可以解决问题:

setTimeout(() => {
 this.setState({list: [{x: "Before Pool", y:60000}, {x: "After Pool", y:30000}]});
 console.log("testing", this.state.text);
   }.bind(this), 2000) ;

The answer is pretty simple: You're looking at the wrong this . 答案很简单:您在看错this

Since you're writing a callback in a closure it's important to know that you can't access the this from outside. 由于您是在闭包中编写回调,因此重要的是要知道您不能从外部访问this It always refers to the current context. 它总是指当前上下文。

As a workaround, define your own variable (typically called self ) to use inside the closure: 解决方法是定义您自己的变量(通常称为self )以在闭包内部使用:

componentDidMount() {
    var self = this; // copy the reference
    setTimeout(() => {
        self.setState({list: [{x: "Before Pool", y:60000}, {x: "After Pool", y:30000}]});
        console.log("testing", this.state.text);
    }, 2000) ;
}

You need to bind the handleChange method here 您需要在此处绑定handleChange方法

<Slider min={0} max={20} defaultValue={3} onChange={this.handleChange} />

This should look like this 看起来应该像这样

<Slider min={0} max={20} defaultValue={3} onChange={this.handleChange.bind(this)} />

Or you can simply use Arrow Functions in the signature of the method, is better to use this all the time to save you to bind all the time. 或者,您可以在方法的签名中简单地使用“箭头函数”,最好一直使用此功能来节省您一直绑定的时间。 It should look like this: 它看起来应该像这样:

handleChange = event => {
    console.log("from handle change", event);
    this.setState({text : event });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM