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

recoil-persist not working as expected with useSetRecoilState in the useEffect hook #64

Open
sarobolket opened this issue Jul 28, 2022 · 1 comment

Comments

@sarobolket
Copy link

I want to update an atom with the query string in the URL. With useSetRecoilState I can set the atom but recoil-persist doesn't save in localStorage. I can see that the onSet callback in the effects of the atom is not triggered.

See this example:

import { atom } from 'recoil';
import { recoilPersist } from 'recoil-persist';

const { persistAtom } = recoilPersist();

export const atomState = atom({
  key: 'example',
  default: null,
  effects: [persistAtom],
});

import React from 'react';
import { Navigate } from 'react-router-dom';
import { useRecoilValue } from 'recoil';
import { atomState } from 'state';
import { useSaveQueryString } from 'hooks/useSaveQueryString';

function MobileRoute() {
  useSaveQueryString ();

  const state = useRecoilValue(atomState );

  if (state ) {
    return <Navigate to="/something" />;
  }

  return <div>Loading...</div>;
}

export default MobileRoute;
function useSaveQueryString () {
  const [search] = useSearchParams();
  const setAtom = useSetRecoilState(atomState);

useEffect(() => {
const searchAsObject = Object.fromEntries(new URLSearchParams(search));

  if (searchAsObject.id) {
    setAtom (searchAsObject.id);
  }
}, [search, setAuth])
}

How can I solve it? Some example with recoil-sync??

@juanevp
Copy link

juanevp commented Oct 28, 2022

I had the same problem, and it turned out that it was the combination of React 18 and strict mode.
Inside an internal Recoil component called RecoilRoot_INTERNAL there is an useEffect() that initializes and cleans up the internal state store When Recoil runs in strict mode that effect is called multiple times and during all this reinitialization all effect subscriptions generated when calling onSet are removed.
So the workaround was to bail out of strict mode.

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

2 participants