question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

reaction with previous value

See original GitHub issue

Welcome to MobX. Please provide as much relevant information as possible!

I have a:

  1. 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:closed
  • Created 5 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
Cianticcommented, Apr 15, 2019

I use this handy helper for reactions:

import { reaction } from "mobx";
import { isEqual } from "lodash";

export const reactionWithOldValue = <T>(
    expression: () => T,
    effect: (newValue: T, oldValue?: T) => void
) => {
    let oldValue: T;
    return reaction(expression, v => {
        if (!isEqual(v, oldValue)) {
            effect(v, oldValue);
            oldValue = v;
        }
    });
};

Note Implementation perhaps leaks memory, since the oldValue is not cleaned.

Usage:

let client = observable("");
reactionWithOldValue(
    () => client,
    (newValue, oldValue) => console.log(newValue, oldValue) // do something with old and new value
);
0reactions
lock[bot]commented, Jul 21, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found