简体   繁体   English

反应setState清空旧状态

[英]React setState empties old state

I'm running into a problem, looking for help. 我遇到问题,正在寻求帮助。 I have a function called mapRanks . 我有一个名为mapRanks的函数。 It gets called in componentWillMount and basically setState of elements using map through array of strings. 它通过map通过字符串数组在componentWillMount和元素的setState中被调用。 It works fine with one array but I need to run it multiple times with multiple different arrays. 它可以在一个数组上正常工作,但是我需要在多个不同的数组上运行多次。

What happens is all the previous state becomes array[0] except the last one. 发生的是,除最后一个状态外,所有先前的状态都变为array[0]

Please take a look at it: 请看一下:

export const mapRanks = (options, ranks, count, element) => {
  component = options.component;   // this component
  group = options.group;           // can be 'mapFirst', 'mapSecond', ...
  let col = count;
  let j = count;
  let k = count;

  const map = ranks.map((rank, i) => {
    if (i === col) {
      col = col + j--;
      if (j === 0) {
        j = k--;
      }
      return [element(i, group, rank), <div key={ i } className="clearfix"></div>];
    }
    return [element(i, group, rank)];
  });

  const newState = update(component.state, { maps: { [group]: { $set: map } } });
  component.setState(newState);

  setTimeout(() => { console.log(component.state.maps); }, 7000);
};

My state looks like this: 我的状态如下所示:

  constructor() {
    super();
    this.state = {
      maps: {
        mapFirst: [],
        mapSecond: [],
        mapThird: [],
        mapFourth: [],
      },
    };
  }

Also, I don't think it's related but I'm wondering... Do you see the setTimeout to console.log above? 另外,我不认为这是相关的,但我想知道...您是否在上面的console.log看到setTimeout I had to use setTimeout because the state was returning array[0] but with setTimeout , it returns the whole array of elements. 我必须使用setTimeout因为状态返回的是array[0]但是使用setTimeout ,它将返回整个元素数组。 Why is that? 这是为什么?

EDIT: I think I found the problem. 编辑:我认为我发现了问题。

  componentWillMount() {
    mapRanks(
      { component: this, state: 'mapFirst' }, ranksFirst, 9, element
    );
    mapRanks(
      { component: this, state: 'mapSecond' }, ranksSecond, 8, element
    );
  }

When I add the second mapRanks , the both (two) console.log that returns has mapFirst[0] and mapSecond[165] .. I still don't understand why. 当我添加第二个mapRanks ,返回的两个(两个) console.log具有mapFirst[0]mapSecond[165] ..我仍然不明白为什么。

2nd EDIT: I fixed it but... 第2编辑:我修复了,但是...

I added setTimeout for the second mapRanks function call and now it works. 我为第二个mapRanks函数调用添加了setTimeout ,现在可以使用了。 I need a better solution. 我需要一个更好的解决方案。 Please help. 请帮忙。

There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains. 无法保证对setState的调用的同步操作,并且可能会为提高性能而批量调用。

Rather than updating the state, diffing the DOM and rendering the changes each time you call setState , React's going to try and batch the calls together and execute them before the next repaint, so that your code doesn't block the UI. 每次调用setState ,它不会更新状态,扩散DOM并呈现更改,而React将尝试将这些调用组合在一起并在下一次重新绘制之前执行它们,以使您的代码不会阻塞UI。

This means that setState can be asynchronous , so you'll need to pass a callback function if you want to wait for the state to be changed before accessing it. 这意味着setState可以是异步的 ,因此,如果要在访问之前等待状态更改,则需要传递一个回调函数。

component.setState(newState, () => {
  console.log(component.state.maps);
});

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

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