title: How to use hook in createSlice of react redux
body: <p>How to use hooks to save data in localStorage in createSlice  of react @reduxjs/toolkit
  When ever I change the theme I want to save the data in localStorage. I know this can done directly with js code with I am having issue with useLocalStorage hook.</p>
  <pre><code>import { createSlice } from &quot;@reduxjs/toolkit&quot;;
  import useLocalStorage from &quot;../hooks/useLocalStorage&quot;;
  
  const initialState = {
      theme: true
  }
  
  const [settings, setSettings] = useLocalStorage(&quot;settings&quot;, initialState);
  
  
  export const settingsSlice = createSlice({
      name: &quot;theme&quot;,
      initialState: { value: initialState },
      reducers: {
          setSettings: (state, action) =&gt; {
              state.value = action.payload;
          },
          toggleTheme: (state) =&gt; {
              const temp = { ...state.value, theme: !state.value.theme };
  
              //save in localStorage
              setSettings(temp);
  
              state.value = temp;
          },
      },
  });
  
  export const { toggleTheme } = settingsSlice.actions;
  
  export default settingsSlice.reducer;
  </code></pre>
  <pre><code>import { useState } from &quot;react&quot;;
  
  function useLocalStorage(key, initialValue) {
      const [storedValue, setStoredValue] = useState(() =&gt; {
          try {
              const item = window.localStorage.getItem(key);
              return item ? JSON.parse(item) : initialValue;
          } catch (error) {
              console.log(error);
          }
      });
  
      const setValue = (value) =&gt; {
          try {
              window.localStorage.setItem(key, JSON.stringify(valueToStore));
          } catch (error) {
              console.log(error);
          }
      };
  
      return [storedValue, setValue];
  }
  
  export default useLocalStorage;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/cBPKt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cBPKt.png" alt="enter image description here" /></a></p>
  <p>I tried to call the hook before updating my data but I got error</p>