码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-29 00:37:09      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:pre   ring   call   city   lin   explore   set   reac   map   

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

标签:pre   ring   call   city   lin   explore   set   reac   map   

原文地址:http://www.cnblogs.com/Answer1215/p/7608961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!