-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
add defaultServerValue property #74
Conversation
Hey, thanks for your contribution! Is it ok for me to ask some questions? I'm not sure I understand. Can you give me a code snippet example that doesn't work with the current API that will work if there is a I'm just trying to imagine your codebase and I can't. For example, I guess I will write the following code if I will need to do what I understand you are describing: const [showModal, setShowModal] = useLocalStorageState('show-modal', {
defaultValue: false
})
if (!showModal) {
return null
}
return <div>modal</div> How should I change the example below so it now needs a new option in order to work property (or you can just show a different example, if that's easier)? Thanks in advance! |
Also, I see two people liked the PR — @kadirgombel and @damla. Do you have the same use case as well? Can you explain your need for such a property? Thanks! |
Hello, they are my coleagues. Example: const showWelcomeModal, setShowWelcomeModal] = useLocalStorageState('welcome-modal', {
defaultValue: true // we want to show welcome if value does not exist in local storage
})
if (!showModal) {
return null
}
return <div>welcome</div> code below has an issue server always shows modal with SSR response but if Thank you! |
Sorry, I still don't understand. I will try to break this down and give you a CodeSandbox that you can edit so we are on the same page. Can you edit this CodeSandbox so you make the modal div flicker? Also, do you know that you can conditionally give a different const isServerOrHydrating = useSyncExternalStore(
() => {
return () => {};
},
() => false,
() => true
);
const [showWelcomeModal, setShowWelcomeModal] = useLocalStorageState('welcome-modal', {
defaultValue() {
if (isServerOrHydrating) {
return "server default value";
}
return "client default value";
},
}) I hope you understand me. I want to understand it before merging it. |
Hello, yep there is no problem. https://codesandbox.io/p/devbox/suspicious-meninsky-forked-qf2pgw
const [showWelcomeModal] = useLocalStorageState("case-2", {
defaultValue: () => {
if (isServerOrHydrating) return false;
return true;
},
}); this will work in users recurrent enters to page without flicker so it is fixing that issue.
I hope it was understandable; thanks for your time. |
Nice! I think I got it. So what you are trying to accomplish is this — https://codesandbox.io/p/devbox/suspicious-meninsky-forked-yz8zfr?workspaceId=d4f0087a-bf2e-4c09-a4d6-c87fd0d77ed8, right? The example shows how to show the modal but not render it on server or while hydrating to avoid flicker and hydration errors. If you manually call That's it right? |
Hello again, I can't access the codesandbox but you are right, If I use const [showWelcomeModal] = useLocalStorageState("case-2", {
defaultValue: () => {
if (isServerOrHydrating) return false;
return true;
},
}); there will be no flicker and hydration error but issue with this is |
Sorry, here's the link again, it should work now — https://codesandbox.io/p/devbox/suspicious-meninsky-forked-yz8zfr?workspaceId=d4f0087a-bf2e-4c09-a4d6-c87fd0d77ed8. |
yep it's what I accomplish thank you! |
I'll merge the PR. After I took the time to understand it, it seems like a good idea. Thanks for your contribution! I will try to make a release soon. |
Thanks a lot for spending your time, |
The release is ready. It was fun for me as well! Your first contribution is really well done, keep the good work! |
Reason
we have a modal to defaults to open if local storage value does not exists. If value for opened before is undefined we want to open modal. using
defaultValue
to set key tofalse
causes modal to flashes on top that. we tried setting different values for client and server indefaultValue
using it as function.code above prevents modal first opening then closing as flash but now causes hydration errors.
fix is using
getServerSnapshot
to differ client and server defaults.getServerSnapshot
function runs during SSR and first hydration so it prevents that errors. See docsWhat is done
defaultServerValue
readme