title: How to save state in localStorage using redux toolkit on nextjs project
body: <p>I want to save my updated state to localStorage but when try this facing an error. <code>localstorage is not defined</code></p>
  <p>cartSlice.jsx</p>
  <pre><code>import {v4 as uuid} from &quot;uuid&quot;;
  import {createSlice} from &quot;@reduxjs/toolkit&quot;;
  const data = JSON.parse(localStorage.getItem('cart'));
  export const cartSlice = createSlice({
    name: 'shoppingCart',
    initialState: data ? data : [],
    reducers: {
      addToCart: (state, action) =&gt; {
          const product = action.payload;
          const productInCart = state.find(item =&gt; item.id === product.id &amp;&amp; item.color === product.color &amp;&amp; item.size === product.size &amp;&amp; item.material === product.material);
  
          if (productInCart) {
              const cartProductIndex = state.findIndex(item =&gt; item.id === product.id);
              state[cartProductIndex].quantity = state[cartProductIndex].quantity + product.quantity;
          } else {
              product['cartId'] = uuid();
              return [product, ...state];
          }
  
          localStorage.setItem('cart', JSON.stringify(state));
      }
  }
  })
  </code></pre>
  <p>cartAction.js</p>
  <pre><code>import {addToCart} from &quot;@slices/cartSlice&quot;;
  
  export const addToCartAction = (payload) =&gt; (dispatch) =&gt; {
   dispatch(addToCart(payload))
  }
  </code></pre>
  <p>How to solve this?</p>