Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid "Setting selectors within atomicUpdate is not supported" when using useRecoilTransaction__UNSTABLE #2289

Open
ezmiller opened this issue Sep 20, 2023 · 0 comments

Comments

@ezmiller
Copy link

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant