Need a little react help
Hello family, its been a while. So for my site I wanted to save a simple count of users visited. I wrote this code:
which works fine but for some reason every page refresh the
updateGlobalStats
is called even when the view has been set in local storage? This should not happen
This is the console output.10 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
If anyone has implemented something like this in convex themselfs let me know please!
Clear your console log, and refresh the page...and see what you get on the first run...
useLocalStorage might not being read correctly because of the extra wrap
defaultValue
try this to make sure value is read from localstorage correctly on component mount:
const [viewCounted, setViewCounted] = useLocalStorage<"no" | "yes">("view_counted", "no");
@CodingWithJamalIt seems to default to no at first even though in local storage its "true" right now
As @Hmza mentioned the initalie value may be incorrect, useLocalStorage('view_counted', 'yes');
the api for default value is not the problem
It seems like the issue might be with how the initial value is being read from local storage.double check if the
defaultValue
option in useLocalStorage
is being set correctly, and ensure that it aligns with the value stored in local storage. You could also log the value right after calling useLocalStorage
to see what it's retrieving on the first render.okay i will check