reaction with previous value
See original GitHub issueWelcome to MobX. Please provide as much relevant information as possible!
I have a:
- Idea:
- What problem would it solve for you?
- Do you think others will benefit from this change as well and it should in core package (see also mobx-utils)?
- Are you willing to (attempt) a PR yourself?
const noPreviousValue = Symbol()
reaction(() => ..., (newValue, previousValue /* noPreviousValue if no previous value*/) => {
})
sometimes the previous value is useful to make decisions on how to react to the change right now we have to do workarounds like
let oldValue = undefined;
reaction(() => ..., (newValue) => {
// do something comparing new with old if needed
oldValue = newValue
})
but it would be cool if it was built-in as part of reaction
if performance/garbage collection is an issue for some reason there could be an option such as
{ trackPreviousValue: boolean} // defaults to false
but since in theory the previous value would be garbage collected as soon as the reaction is done running I don’t see why it would be at first
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (12 by maintainers)
Top Results From Across the Web
Running side effects with reactions - MobX
For the "Now I'm hungry" function, this is every time the isHungry computed changes, only one time. Reaction. Usage: reaction(() => value, (value,...
Read more >How do I get the previous value of a field in React? I want to ...
How do I get the previous value of a field in React? I want to use it to display the user's previous input...
Read more >Use previous value through a React hook
To use the previous stored value of a variable, you can create a hook called usePrevious() combining React's useRef() and useEffect() hooks.
Read more >Remembering the previous value of a prop or a state while ...
Remembering the previous value of a prop or a state while using React Hooks. Prior to the introduction of hooks, if you want...
Read more >Comparing Previous useEffect Values in React
With functional components now being the standard in React, we lost one notable perk of using the lifecycle hooks (such as ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I use this handy helper for reactions:
Note Implementation perhaps leaks memory, since the oldValue is not cleaned.
Usage:
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.