【问题标题】:Cannot read property 'setState' of undefined ReactJS无法读取未定义 ReactJS 的属性“setState”
【发布时间】:2018-12-16 21:07:42
【问题描述】:

我试图在我的函数中设置 React 中的状态,但是我收到一条错误消息,指出:无法读取未定义的属性“setState”。

下面是我的代码,我在构造函数中调用状态,然后在 addTimes 函数中设置状态并将其绑定到该函数,但是我仍然收到错误。

class Settings extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      times: []
    };

  }
  render(){
    Array.prototype.remove = function() {
        var what, a = arguments, L = a.length, ax;
        while (L && this.length) {
            what = a[--L];
            while ((ax = this.indexOf(what)) !== -1) {
                this.splice(ax, 1);
            }
        }
        return this;
    };

    var currentTicked = [];
    var times =[]
    function addTimes(id){
      var index = times.indexOf(id);
      if (!times.includes(id)) {
        $("input:checkbox[name=time]:checked").each(function(){
          currentTicked.push($(this).val());
          times = times.concat(currentTicked)
          times = jQuery.unique(times);
        });
      } else if(times.includes(id)){
        times.remove(id)
      }
      console.log(times);
      addTimes = () => {
        this.setState({
          times: times
        });
      }
    }

【问题讨论】:

  • 这对我来说很奇怪` addTimes = () => { this.setState({ times: times }); }`。为什么不直接放 `this.setState({ times: times });`
  • 还有为什么要在每次渲染的Array的原型中重新定义函数remove

标签: javascript reactjs


【解决方案1】:

您还没有将函数绑定到类。试试看

addTimes = (id) => {
  // code here
}

在组件类中

【讨论】:

    【解决方案2】:

    用箭头函数试试:

    addTimes = id => {
          var index = times.indexOf(id);
          if (!times.includes(id)) {
            $("input:checkbox[name=time]:checked").each(function(){
              currentTicked.push($(this).val());
              times = times.concat(currentTicked)
              times = jQuery.unique(times);
            });
          } else if(times.includes(id)){
            times.remove(id)
          }
          console.log(times);
          addTimes = () => {
            this.setState({
              times: times
            });
          }
        }
    

    或者,像这样绑定thisin addTimes函数:

    constructor(props) {
        super(props);
        this.state = {
          times: []
          this.addTimes = this.addTimes.bind(this);
        };
    
      }
    

    【讨论】:

      【解决方案3】:

      最好使用 es6 语法。试试下面的代码。

       class Settings extends React.Component {
          constructor(props) {
           super(props);
           this.state = {
             times: []
            };
      
          let times = [];
          let currentTicked = [];
          this.addTimes = this.addTimes.bind(this);
          }
           addTimes(id) {
              let index = times.indexOf(id);
              if (!times.includes(id)) {
                  $("input:checkbox[name=time]:checked").each(function(){
                    currentTicked.push($(this).val());
                    times = times.concat(currentTicked)
                    times = jQuery.unique(times);
                  });
                } else if(times.includes(id)){
                  times.remove(id)
                }
                this.setState({
                  times: times
                });
          }
          render(){
          this.addTimes;
          return (
              Array.prototype.remove = function() {
                  var what, a = arguments, L = a.length, ax;
                  while (L && this.length) {
                      what = a[--L];
                      while ((ax = this.indexOf(what)) !== -1) {
                          this.splice(ax, 1);
                      }
                  }
                  return this;
              }
          );
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-23
        • 2018-09-11
        • 2017-07-09
        • 1970-01-01
        • 2018-03-24
        • 1970-01-01
        • 2018-07-25
        相关资源
        最近更新 更多