In order to persist the value of a useState
hook in JavaScript, you can make use of the localStorage
API.
539 chars24 lines
In this example, we initialize the count
state with an IIFE that retrieves the value from localStorage, or defaults to 0 if there is no value. Whenever the count
state updates, we store the new value in localStorage using the useEffect
hook with count
as a dependency.
gistlibby LogSnag