title: Updating useReducer 'state' using useEffect
body: <p>In my application, I am using React Hooks/Context API. Now I need to assign fetched data from localStorage to initialState.carts / state.carts whenever my Provider component did mount. It would possible if useEffect supports returning objects. But it's not possible to return object in useEffect!</p>
  
  <p>Now how can I solve the problem?</p>
  
  <p>Code is below,</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const initialState = {
    books: books,
    carts: []
  };
  
  export const Context = createContext(initialState);
  
  export const Provider = ({ children }) =&gt; {
    const [state, dispatch] = useReducer(reducer, initialState);
  
    useEffect(() =&gt; {
      if (localStorage.getItem("carts") !== null) {
        const fetchedCarts = JSON.parse(localStorage.getItem("carts"));
        //Here I want to assign 'fetchedCarts' array items in 'state.carts' or 'initialState.carts'
      }
    });</code></pre>
  </div>
  </div>
  </p>