I have some state management code using Recoil where I'm running into the error:
Setting selectors within atomicUpdate is not supported
I understand how this error is occurring. I have some atoms that follow the pattern of using selectors to set default values like this:
export const productState = atom<Nullable<ProductDrop>>({
key: 'productstate',
default: selector({
key: 'productstate/default',
get: ({ get }) => get(...async selector...)
})
})
The problem occurs when I use useRecoilTransaction__UNSTABLE later to try to set this selector, e.g.:
const setProductState = useRecoilTransaction__UNSTABLE(({ set }) => newState => {
set(productState, newState)
})
When this transaction setter is used, the above error occurs, so long as the default value of the atom is set by a selector. Here is a minimal reproducible example: link
My understand of this so far is that this is simply not allowed, although I'm not sure I. understand why.
What I'd like to know is whether there is any way around this. How can I both ensure a transactional update to a set of pieces of state (what useRecoilTransaction allows) and set my atom state by default. (Note: I tried to use Atom Effects here briefly, but I couldn't get the error handling to work properly. The effects seemed to swallow errors).