react的this.setState中的坑

react的this.setState中的有两个。

1、this.setState异步的,不能用同步的思维讨论问题

2、在进行组件通讯的回调的时候,this指向子组件,没有指向父亲这,怎么办呢。在

class gradingView extends React.Component {
  constructor(...args) {
    super(...args);
    this.state = {
      suffixIcon: <Icon type="up" />,
      popDivShow:false,
      popImage:jdSelectUp
    };
    window.that=this;
  }

在这里面将that

还有一种方案。

在回调函数中写

  onSelectedItem=(item)=>{
    var that=this;
    that.setState({currentItem:item},function(){
      that.fetchData(0);
    });
  }

为什么这样写呢,因为这里是es6的开发环境,与编译器有关。

原文地址:https://www.cnblogs.com/sexintercourse/p/12001067.html