React 中的坑 https://github.com/justjavac/react-pits/issues
React 技术交流群: 342685917
React 中的坑
Home Page: https://github.com/justjavac/react-pits/issues
License: GNU General Public License v3.0
React 中的坑 https://github.com/justjavac/react-pits/issues
React 技术交流群: 342685917
React has a setState() problem: Asking newbies to use setState() is a recipe for headaches. Advanced users have secret cures.
为了性能和其它原因,setState
这个 API 很容易被误用。
setState
不会立刻改变 React 组件中 state 的值setState
通过引发一次组件的更新过程来引发重新绘制setState
函数调用产生的效果会合并问题
看如下代码:
// state.count 当前为 0
this.setState({count: state.count + 1});
this.setState({count: state.count + 1});
this.setState({count: state.count + 1});
// state.count 现在是 1,而不是 3
三次操作被合并为了一次。
解决方式
this.setState((prevState, props) => ({
count: prevState.count + props.increment
}));
参考资料
The
SyntheticEvent
is pooled. This means that theSyntheticEvent
object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
在 React 事件处理中,事件对象被包装在一个 SyntheticEvent
(合成事件)对象中。这些对象是被池化的(pooled),这意味着在事件处理程序会把这些对象重用于其他事件以提高性能。随之而来的问题就是,异步访问事件对象的属性是不可能的,因为事件的属性由于重用而被重置(nullified)。
下面代码存在问题:
function handleClick(event) {
setTimeout(function () {
console.log(event.target.name);
}, 1000);
}
控制台会输出 null
,因为每次事件回调完成后,SyntheticEvent
会被重置。
解决方式是把 event
赋值到一个内部变量上。
function handleClick(event) {
let name = event.target.name; // 内部变量保存 event.target.name 的值
setTimeout(function () {
console.log(name);
}, 1000);
}
facebook 官方的实例:
function onClick(event) {
console.log(event); // => nullified object.
console.log(event.type); // => "click"
const eventType = event.type; // => "click"
setTimeout(function() {
console.log(event.type); // => null
console.log(eventType); // => "click"
}, 0);
// Won't work. this.state.clickEvent will only contain null values.
this.setState({clickEvent: event});
// You can still export event properties.
this.setState({eventType: event.type});
}
如果想异步访问事件属性,可以在事件上调用 event.persist()
,这会从池中移除合成事件并允许对事件的引用被保留。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.