mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-24 04:33:10 +00:00
* remove event pooling and SyntheticEvent#persist from documents Syntheticevent#event exists in v17, but it does nothing at the version * add a page for legacy event pooling for _redirects * add a warning e.persist() is no longer pooled * Update legacy-event-pooling.md * docs: update a redirect link for event pooling * Update legacy-event-pooling.md * Update legacy-event-pooling.md * Update reference-events.md Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
1.0 KiB
1.0 KiB
id, title, permalink
| id | title | permalink |
|---|---|---|
| legacy-event-pooling | Event Pooling | docs/legacy-event-pooling.html |
Note
This page is only relevant for React 16 and earlier, and for React Native.
React 17 on the web does not use event pooling.
Read more about this change in React 17.
The SyntheticEvent objects are pooled. This means that the SyntheticEvent object will be reused and all properties will be nullified after the event event handler has been called. For example, this won't work:
function handleChange(e) {
// This won't work because the event object gets reused.
setTimeout(() => {
console.log(e.target.value); // Too late!
}, 100);
}
If you need to access event object's properties after the event handler has run, you need to call e.persist():
function handleChange(e) {
// Prevents React from resetting its properties:
e.persist();
setTimeout(() => {
console.log(e.target.value); // Works
}, 100);
}