[React] Call setState with null to Avoid Triggering an Update in React 16

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React] Call setState with null to Avoid Triggering an Update in React 16相关的知识,希望对你有一定的参考价值。

Sometimes it’s desired to decide within an updater function if an update to re-render should be triggered. Calling .setState with null no longer triggers an update in React 16. This means we can decided if the state gets updated within our .setState method itself!

In this lesson we‘ll explore how this works by refactoring a city map app that updates even if you choose the same map twice.

 

  selectCity = evt => {
    const newValue = evt.target.value;
    this.setState(state => {
      if (state.city === newValue) {
        return null;
      }
      return {
        city: newValue
      };
    });
  };

 

以上是关于[React] Call setState with null to Avoid Triggering an Update in React 16的主要内容,如果未能解决你的问题,请参考以下文章

React小结

如何使用fetch(GET Call)设置超时react-native api调用?

[踩坑]Flutter使用setState出错call setState() on a State object for a widget that no longer appears in th

[踩坑]Flutter使用setState出错call setState() on a State object for a widget that no longer appears in th

[踩坑]Flutter使用setState出错call setState() on a State object for a widget that no longer appears in th

react setState