This repository was archived by the owner on Jan 1, 2025. It is now read-only.
Replies: 2 comments 3 replies
-
|
Something like this maybe? export function useInitialRecoilState<T>(state: RecoilState<T>, optional?: T) {
const [value, setValue] = useRecoilState(state);
const [optionalValue, setOptionalValue] = useState([value, optional]);
useEffectOnce(() => {
if (optional === value) {
setOptionalValue(undefined);
} else {
setValue(optional);
}
});
useUpdateEffect(() => {
setOptionalValue(undefined);
}, [value]);
return [
optionalValue === undefined || value !== optionalValue[0]
? value
: optionalValue[1] ,
setValue,
];
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
The default value for some atom state is managed by the const someState = atom({
key: 'MyModule/SomeState',
default: 'hello world',
});
function Component(){
const [state, setState] = useRecoilState(someState);
// "hello world" is immediately on first render
return <div>{state}</div>;
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
Is there any reason why the
useRecoilStatehook doesn't take an optional default value likeuseStatedoes?This would solve a lot of issues such as making data available on initial render and also hydrating data from the server.
In effect this would work like you had set the state with
useSetRecoilStatein a hook, but the benefit would be the value being available on initial render.Is there any issue that I'm not seeing?
Beta Was this translation helpful? Give feedback.
All reactions