old-ybbond

My old site that was written with ReactJS
Log | Files | Refs | README

commit cf1b70202e2e81c144e386ac70dd73bd9344fb5c
parent 710641b13087c94d6252ec13bea60749ece47480
Author: Yohanes Bandung <bandungpenting@gmail.com>
Date:   Mon,  9 Mar 2020 02:33:49 +0700

refactor(theme): store dark to localStorage

Diffstat:
Msrc/App.js | 12+++++++++++-

1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/App.js b/src/App.js
@@ -31,7 +31,17 @@ const InnerWrapper = styled.div`
 
 const App = () => {
   const [dark, setDark] = React.useState(true);
-  const toggleDark = () => setDark(!dark);
+
+  React.useEffect(() => {
+    const isDark = localStorage.getItem('dark') === 'true';
+    setDark(isDark);
+  }, []);
+
+  const toggleDark = () => {
+    const newState = !dark;
+    localStorage.setItem('dark', JSON.stringify(newState));
+    setDark(newState);
+  };
 
   return (
     <ThemeProvider theme={{mode: dark ? 'dark' : 'light'}}>