Skip to content

feat: Persist theme in localStorage#94

Open
iamdarshshah wants to merge 1 commit into
siddharthkp:mainfrom
iamdarshshah:feat/theme
Open

feat: Persist theme in localStorage#94
iamdarshshah wants to merge 1 commit into
siddharthkp:mainfrom
iamdarshshah:feat/theme

Conversation

@iamdarshshah

@iamdarshshah iamdarshshah commented May 25, 2020

Copy link
Copy Markdown
Contributor

[ 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]
}
 

@vercel

vercel Bot commented May 25, 2020

Copy link
Copy Markdown

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

@iamdarshshah

Copy link
Copy Markdown
Contributor Author

@siddharthkp & @rubenmoya Any updates or feedback on this?

@siddharthkp siddharthkp changed the base branch from master to main June 27, 2020 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant