Atom Default Value derived from prop? #1168
Replies: 3 comments 6 replies
-
The default value itself can't come from a prop because the same atom could be used in multiple components which could pass in different values. One option could be to store a placeholder value as the default and use the prop as a fallback in the component. e.g.: const myAtom = atom({key: 'MyAtom', default: null});
function MyComponent({defaultValue}) {
const value = useRecoilState(myAtom) ?? defaultValue;
...
} However, it is also possible to initialize atoms outside of the React component hierarchy. If you are having trouble using the |
Beta Was this translation helpful? Give feedback.
-
The issue is while your example works fine on the client side it fails on SSR (ReactJS.net). I Assume the Recoil state setting is wrapped in a useEffect(). The issue is once i start moving things into atoms for the shared state my SSR would be completely broken as it would render empty values everywhere because the initials come through as Props from MVC/ReactJS.net > React. For now my work around is to create the call a helper function they checks if a given atom exists in a global object and if so use it otherwise create it and pass in the prop as the default value. Not Ideal but is the only way i could get Recoil to have atoms work with SSR. |
Beta Was this translation helpful? Give feedback.
-
Also the initializeState approach worked but our react is setup very dynamically where we have an entry point App.jsx (where RecoilRoot is) that receives various props like (Header, Body, Footer, Additional Components, etc..), This props are completely dynamic and would have their own atoms that need to be created. So I would have to init every possible atom for every possible combination. again not ideal |
Beta Was this translation helpful? Give feedback.
-
Is there any way to initialize an atom with its default value coming from a Prop?
We currently are using ReactJS.net for SSR. While I understand SSR isnt support yet do you have an idea when this will be? Currently it looks like the setRecoilState calls are not done for SSR so the response just comes in with blank values for places that were using the atoms.
I have tried to use the initializeState on the but our app setup has a very deeply nested tree structure and very dynamic so its difficult to try and set initializeState on the because the children views change.
Beta Was this translation helpful? Give feedback.
All reactions