正在努力获得一个滑块来更改React状态下“ text”的值。

不断出现错误:

尽管我尽了最大的故障排除努力,但“ App.js:90 Uncaught TypeError:this.setState不是一个函数”。

解决办法是什么?

  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>


javascript - SetState不是OnChange上的函数-LMLPHP
javascript - SetState不是OnChange上的函数-LMLPHP

最佳答案

您需要绑定handleChange方法

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

07-24 19:19
查看更多