Local storage - x-data showing old/wrong value after page refresh #1210
-
I am trying to toggle dark mode saving the value in local storage. But when I toggle the dark mode on and off and refresh the page, the value in the local storage shows 'false' (which is expected) but the x-data sets the wrong value which is 'true'.
Check this fiddle for demo: https://jsfiddle.net/darpan_kulkarni/kpr5de0a
What I am doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
localStorage saves and retrieves everything as strings. The string |
Beta Was this translation helpful? Give feedback.
-
Thank You @SimoTod for mentioning the obvious problem; However, this way I implemented dark mode with alpine.js
Now, in Blade Template File;
Now you can able to maintain the state of dark mode. Cheers!!! |
Beta Was this translation helpful? Give feedback.
localStorage saves and retrieves everything as strings. The string
false
is a truthy value so it works as expected.You need to change your x-data to
x-data="{darkMode: localStorage.getItem('dark') === 'true'}"