useEffect not updating with array #2807
Replies: 3 comments 3 replies
-
simple debug// packages/react-reconciler/src/ReactFiberHooks.new.js
function updateEffectImpl(fiberFlags, hookFlags, create, deps): void {
const hook = updateWorkInProgressHook();
const nextDeps = deps === undefined ? null : deps;
let destroy = undefined;
if (currentHook !== null) {
const prevEffect = currentHook.memoizedState;
destroy = prevEffect.destroy;
if (nextDeps !== null) {
const prevDeps = prevEffect.deps;
if (areHookInputsEqual(nextDeps, prevDeps)) { 👈🏻 // <- Here is the key of your problem
hook.memoizedState = pushEffect(hookFlags, create, destroy, nextDeps);
return;
}
}
} You can noticed that the Value vs. ReferenceWhen you change the array value , the solution+ const arr = array.slice(0);
useEffect(() => {
console.log(arr);
}, [arr]); or other
|
Beta Was this translation helpful? Give feedback.
-
See also: https://mobx.js.org/react-integration.html#useeffect |
Beta Was this translation helpful? Give feedback.
-
@davetapley sorry, sir. Maybe I am misleading you by my poor english. const View = observer(({ model: { array, bool } }: ViewProps) => { 👈🏻 use case only mobx and mobx-state-tree https://jsfiddle.net/ichenlei/ace458dw/24/ previous commnet contain some mistake info, so I hide my comment. |
Beta Was this translation helpful? Give feedback.
-
I've now convinced myself this isn't a bug, but I created a repro ⬇️ in a sandbox prior to realizing what was happening, so I thought others might benefit from seeing it 👀
https://codesandbox.io/s/mobx-observable-array-with-react-lfh61?file=/src/App.tsx
The real gotcha for me was that I'd incorrectly assumed that MobX would bring some magic that would allow changes to the array to cause the
useEffect
to trigger.I don't know why I assumed this 🤪
Beta Was this translation helpful? Give feedback.
All reactions