atomWith: Storage And Compare #2715
-
I'd like to create an atomWithStorageAndCompare that
The goal is to prevent rerenders after db changes if the actual atom value didn't change. I assume it's not possible with atomWithStorage and that I have to create my own atomWithStorageAndCompare. However, I'm unable to achieve that. Any help is much appreciated 🙏 -- const myAtom = atomWithStorage(
'myAtom',
[],
{
getItem: getFromDB,
setItem: () => {}, // not necessary for this example
removeItem: () => {}, // not necessary for this example
subscribe: (key, callback, initialValue) => {
const listener = addDatabaseChangeListener(async () => {
// compare previous and next values
const prevValue = "?" // how to get this value 🤔
const nextValue = await getFromDB()
if (areEqual(prevValue, nextValue)) return
callback(nextValue)
})
return () => {
listener.remove()
}
},
},
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In general, yes.
In your case, what would be easier it to use |
Beta Was this translation helpful? Give feedback.
In general, yes.
In your case, what would be easier it to use
atomWithStorage
andselectAtom
. Doesn't it work?