facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.5k stars 1.18k forks source link

syncEffect read not called initially when having hash in url #2112

Open filipac opened 1 year ago

filipac commented 1 year ago

Hello,

I use RecoilURLSyncJSON to sync an atom to the url and have a custom read and write set for the syncEffect.

The problem I discovered is that when I have an url with a hash at the end (eg: using HashRouter from react-router-dom), the read function is not called.

Example: if the url is https://example.com/?hr-brand_id=[3254]#/ the read function is not called BUT if the url is https://example.com/?hr-brand_id=[3254] the read function is called as expected and the state is read from the URL.

Is there something I can do to make it call even if there is something in window.location.hash ? The browser correctly shows those 2 separated: image

Initially I though json parsing fails because #/ at the end is not valid JSON, but if parsing would be done using window.location.search, the parsing would have no problems even with a hash in the url.

simon-lund commented 1 year ago

You should be able to specify which part of the URL is used via the location prop: https://recoiljs.org/docs/recoil-sync/api/RecoilURLSync#url-location.

But if that doesn't work, you can also write your own serializer: https://recoiljs.org/docs/recoil-sync/api/RecoilURLSync#custom-serialization