react-ui
react-ui copied to clipboard
feat: Persist theme in localStorage
[ Reference #91 ]
Custom Hook for the theme change that will update the theme globally by adding it to the localStorage.
This will help us to persist the selected theme in local storage and use it every time the page is refreshed.
Let me know your thoughts. Thanks
Here is the code for custom React hook:
const useThemeState = (defaultValue, key) => {
const [value, setValue] = React.useState(() => {
const theme = window.localStorage.getItem(key)
return theme !== null ? JSON.parse(theme) : defaultValue
})
React.useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(value))
}, [key, value])
return [value, setValue]
}
This pull request is being automatically deployed with Vercel (learn more). To see the status of your deployment, click below or on the icon next to each commit.
🔍 Inspect: https://vercel.com/sid/react-ui/cmyz07wbv ✅ Preview: https://react-ui-git-fork-iamdarshshah-feat-theme.sid.now.sh
@siddharthkp & @rubenmoya Any updates or feedback on this?