Make value captured in recipe available in parent scope?
tippfelher opened this issue Β· comments
πββ Question
I want to capture the value inside a recipe and have its value available after the Updater called the recipe. (see repro below)
Relevant code:
function becomeOlder() {
var captureAge = 0;
updatePerson((draft) => {
draft.age++;
captureAge = draft.age;
console.log("before", captureAge);
});
console.log("after", captureAge);
}
Actual output example:
before 36
after 0
Wanted output example
before 36
after 36
Is that somehow possible?
Link to repro
Here is the corresponding playground.
Environment
We only accept questions against the latest Immer version.
- Immer version: 9.0.12
- useImmer version: 0.9.0
- Occurs with
setUseProxies(true)
- Occurs with
setUseProxies(false)
(ES5 only) - I β€οΈ immerjs
No it isn't, variables are fundamental feature of JavaScript which Immer can't interact with. Note that you could just have const captureAge =....
inside the draft to shadow the outer one.
Nvm, problem is gone and has nothing to do with immerjs but useImmer. I just implemented useBetterImmer which allows me to access the nextstate.