0

I have a shopping cart website and cart of user stored in localStorage.

now, I want to initial my store with my localStorage when my page will refresh but I get this error:

localStorage is not defined

cart.reducer.js:

import { cartConstants } from '../constants';
import { cartHelper } from '../helpers'

const initialState = []

function cart(state = initialState, action) {
  if(state.length == 0){
    state = cartHelper.getCart(); // error line
  }

  switch (action.type) {
    case cartConstants.CART:
      return action.cart

    default:
      return state
  }
}


export default cart
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
  • Possible Duplicate of [where-to-write-to-localstorage-in-a-redux-app](https://stackoverflow.com/questions/35305661/where-to-write-to-localstorage-in-a-redux-app) – Shubham Khatri Apr 01 '18 at 14:07
  • Are you running this code in browser? Or is this server side rendered application? Also can you share the code where you localStorage? – varunvs Apr 01 '18 at 16:37

0 Answers0