read-react-source-code
read-react-source-code copied to clipboard
Record what I learn after reading React source code
我正在准备研究本教程,但发现最后更新时间为五年前。虽然后续版本没有什么太多破坏性的改动,但我不是很清楚是否会产生什么障碍。望解读!
你好,看完这篇文章,我有两个问题请教一下,还请解答: 1. setState的异步问题 ```js *** onClick(){ this.setState(partialState); this.setState(partialState); } *** ``` 每次调用`setState`时,会调用父类的`setState`,此时会进入`ReactUpdateQueue`中,然后会把`partialState`存放到`ReactCompositeComponent`实例对象的一个队列中,经过一系列步骤,`_processPendingState`该方法会对`partialState`,然后进入到`render`中。 在这一系列调用过程中,我没有看到哪一步会停止对第一个setState退出当前调用栈,肯定就不会去执行第二个setState,即`_processPendingState`该方法处理的队列中只有一个`partialState`,那就谈不上`Object.assign`会把第二个覆盖第一个state。因为js是单线程,而且底层没有发起异步操作。所以有点疑问 2. setState里面的updater ```js function ReactComponent(props, context, updater) { this.props = props; this.context = context; this.refs = emptyObject;...