-
Notifications
You must be signed in to change notification settings - Fork 49.5k
Closed
Description
For practical reasons about performance and energy conservation (in this sense, all React components are non-idempotent and have side effects), I occasionally will check my state before setting it:
if (this.state.something) {
this.setState({ ... });
}
I would like to be able to do this with the transactional version of setState
as well:
this.setState(state => {
if (state.something) {
return { ... };
}
// if null or undefined (or false?) is returned, don't re-render!
});
A toy example of this is a piece of component state that gets updated during each scroll event. Due to event batching (especially with RN), multiple scroll events can fly in during the same batch.
<ScrollView onScroll={() => {
this.setState(state => {
if (state.numberOfScrollEvents < 10) {
return { numberOfScrollEvents: state.numberOfScrollEvents + 1 };
}
});
} />
Metadata
Metadata
Assignees
Labels
No labels