小编Syb*_*ric的帖子

如何在反应组件构造函数中设置setInterval?

主要组件时我需要setInterval.我尝试在构造函数中设置它

  constructor(props) {
    super(props);
    this.props.fetchUserInfo();
    this.props.fetchProducts();
    setInterval(console.log('1'), 1000);
  }
Run Code Online (Sandbox Code Playgroud)

或者里面 componentDidMount

  componentDidMount = () => {
    setInterval(console.log('1'), 1000);
  };
Run Code Online (Sandbox Code Playgroud)

但它总是记录'1'一次.如何正确启动间隔?

javascript reactjs

3
推荐指数
1
解决办法
6027
查看次数

使用......传播,但redux仍然会对状态突变发出警告

Redux在发送时抛出警告:

Error: A state mutation was detected inside a dispatch, in the path: 
roundHistory.2.tickets. Take a look at the reducer(s) handling the action {"type":"ARCHIVE_TICKETS","roundId":"575acd8373e574282ef18717","data":[{"_id":"575acd9573e574282ef18718","value":7,"user_id":"574c72156f355fc723ecdbbf","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575acd9573e574282ef18719","value":9,"user_id":"574c72156f355fc723ecdbbf","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575acd9573e574282ef1871a","value":8,"user_id":"574c72156f355fc723ecdbbf","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575acdac73e574282ef1871b","value":19,"user_id":"574c72156f355fc723ecdbbf","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575ad50c4c17851c12a3ec23","value":29,"user_id":"57522f0b1f08fc4257b9cbc6","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575ad50c4c17851c12a3ec24","value":28,"user_id":"57522f0b1f08fc4257b9cbc6","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575ad
Run Code Online (Sandbox Code Playgroud)

唯一的减速机处理ARCHIVE_TICKETS动作是这样的:

case 'ARCHIVE_TICKETS' :
  var archive = [...state.roundHistory];
  for (var i in archive) {
    if (archive[i]._id === action.roundId) {
      archive[i].tickets = action.data;
    }
  }
  return Object.assign({}, state, {
    roundHistory: archive
  });
Run Code Online (Sandbox Code Playgroud)

如果我使用[...传播],它怎么能改变状态呢?

javascript reactjs redux react-redux

3
推荐指数
1
解决办法
3227
查看次数

标签 统计

javascript ×2

reactjs ×2

react-redux ×1

redux ×1