useProxy error: 'set' on proxy: trap returned falsish #137
Replies: 1 comment 1 reply
-
Thanks for opening the discussion. First things first, So, const useNotWorking = (id) => {
useProxy(store);
if (!store[id]) store[id] = `huhu ${id}`;
return store[id];
}; becomes const useNotWorking = (id) => {
const snap = useSnapshot(store);
if (!snap[id]) snap[id] = `huhu ${id}`;
return snap[id];
}; and this causes the error, because Now, the rationale behind this is render function should be pure and store[id] = 'foo' is mutating an object, which is impure. Although, the lazy init is allowed. So, if (!store[id]) store[id] = 'foo' is actually okay in React. So, it's more or less a limitation in valtio. What's an idiomatic way to fix this. Mutation should be done in useEffect. So, const useIdiomatic = (id) => {
useProxy(store);
useEffect(() => {
if (!store[id]) store[id] = `huhu ${id}`;
})
return store[id];
}; this is something looking good and should work with But const useIdiomaticBetter = (id) => {
useProxy(store);
const thing = store[id] || `huhu ${id}`;
useEffect(() => {
if (store[id] !== thing) store[id] = thing;
})
return thing;
}; is good. This is often a technique with custom hooks. Oh dear, this is so complicated... |
Beta Was this translation helpful? Give feedback.
-
I might found a bug with useProxy, but I am not sure: https://codesandbox.io/s/nameless-hill-xj6o6?file=/src/App.js:0-701
There is also an issue from 2016, but I don't know how accurate it is :-D
GoogleChrome/proxy-polyfill#20
Can anyone comment on it ?
Cheers!
If I run it with CRA I get the error:
TypeError: 'set' on proxy: trap returned falsish for property '1'
on codesandbox it renders, but never sets the value.
Beta Was this translation helpful? Give feedback.
All reactions