关于react的setState的updater
发布于 5 年前 作者 msforest 1969 次浏览 来自 问答
function ReactComponent(props, context, updater) {
  this.props = props;
  this.context = context;
  this.refs = emptyObject;
  this.updater = updater || ReactNoopUpdateQueue;
}
ReactComponent.prototype.setState = ...

// 我找到new组件的时候会注入一个updater
var ReactCompositeComponent = {
  mountComponent(){
    var inst = new Component(publicProps, publicContext, updateQueue);
  }
}

// 如果是按照以上方式传递
class Test extends Component{
  constructor(props){
    super(props); // 这里我一般不写完整
  }
}
/**
 * 所以问题就在这里,这里不传入第三个参数,为啥setState里面的this.updater还能正常用,求解答
 * 是注入的地方没找到??
 */
1 回复

因为this.updater = updater || ReactNoopUpdateQueue;

回到顶部