-
Notifications
You must be signed in to change notification settings - Fork 90
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
fix: store dark/light theme mode in localStorage
#418
fix: store dark/light theme mode in localStorage
#418
Conversation
…com/echelonnought/open-bus-map-search into fix/persist-dark-mode-local-storage
src/layout/ThemeContext.tsx
Outdated
const { i18n } = useTranslation() | ||
const { setItem, getItem } = useLocalStorage('isDarkTheme') | ||
const [isDarkTheme, setIsDarkTheme] = useState(() => { | ||
return getItem() === 'true' || false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder -
if we change return getItem() === 'true' || false
to just return getItem()
will it still work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or even simplier: const [isDarkTheme, setIsDarkTheme] = useState(getItem())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh yeah, that's correct it can be simplified as such
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more thought i have now: do we have to convert the isDarkTheme value to string before storing it at localStorage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is an interesting question @shootermv, I was converting that to string because i think local storage stores both it's values and keys as strings, so i wanted to maintain that flow of data. i could be wrong though. what do you think?
src/layout/ThemeContext.tsx
Outdated
useEffect(() => { | ||
const storedTheme = getItem() === 'true' | ||
setIsDarkTheme(storedTheme || false) | ||
}, []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can be removed but you also should change const [isDarkTheme, setIsDarkTheme] = useState(getItem())
to
const [isDarkTheme, setIsDarkTheme] = useState(getItem() === 'true')
since getItem()
returns string not boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regardless, we can still set the getItem() in setIsDarkTheme within the useEffect hook and it would still work fine @NoamGaash and @shootermv.
Like so:
useEffect(() => {
const storedTheme = getItem()
setIsDarkTheme(storedTheme)
}, [])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use useMemo
, not useEffect
+state
https://stackoverflow.com/questions/56028913/usememo-vs-useeffect-usestate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you know what? why would you need it to be a state variable?
you can simply use:
const isDarkTheme = getItem() === 'true'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for chiming in this late @NoamGaash. Had issues with power where I'm at.
I am reading up the resource you shared from stack overflow, and I have some questions though;
Is it proper to not use useEffect when dealing or working with external web apis like local storage which are necessary side effects or useMemo handles this too properly?
If useMemo does that, then it'd be a good option I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@echelonnought I think each case should be considered individually, but generally speaking -
- It's good to avoid unnecessary renders. Every
setState
makes the component rerender, so a) it's not very elegant and b) the first render has the "wrong" state value - When one state variable based on information from another variable, most probably that it shouldn't be a state variable
- I prefer using
useMemo
in case that the variable is a literal object/array (it means it will have different pointer each render). in your case, it's a primitive value, so you don't even need the memoization part.
That's why I'm in favor using const isDarkTheme = getItem() === 'true'
, and not const isDarkTheme = useMemo(getItem() === 'true', [getItem()])
or something similar.
That's my opinion, hope it answered your question
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah those are pretty solid reasons. especially the useMemo part, if i had my way, i would use useRef to avoid rerenders, i tried but ran into some issues, i was storing those values in local storage but it didn't change with the toggle button, however since we are changing the UI through the theme change, I find it appropriate to use useState instead, to handle that whenever the UI changes. but I pretty much agree with you on every point you listed!
I am replying late because i've had power issues and trying to catch up with work too.
I'll join the slack and discord to follow with the conversations on this!
Thank you, @NoamGaash !
@echelonnought do you need a help with solving a confilcts? |
localStorage
Yeah I am very much interested in resolving these conflicts, had power outage. |
Did you joined our discord chanel?
…On Thu, Jan 25, 2024, 02:17 Ngole Lawson ***@***.***> wrote:
@echelonnought <https://github.com/echelonnought> do you need a help with
solving a confilcts?
Yeah I am very much interested in resolving these conflicts, had power
outage.
—
Reply to this email directly, view it on GitHub
<#418 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAKGMHUEDVPSZQB2DT4UAM3YQGQDJAVCNFSM6AAAAABCDYLC46VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBZGEZTONBSGU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@shootermv I think he's an English speaker |
for merging the latest changes all you need - is to run: |
…x/persist-dark-mode-local-storage
Hi @echelonnought , I made some suggestions. Let me know what you're thinking |
…x/persist-dark-mode-local-storage
Saw the types enforced in the useLocalStorage file, also noticed that you took out the useEffect hook. Thanks on that, care more to explain on the latter decision please @NoamGaash, I mean wouldn't it hurt performance ? |
Hi @echelonnought ! |
My bad, I forgot to push the latest commit 😅 |
Description
Persisted themes to localStorage
screenshots
Closes #383