如何从localStorage设置初始initialState的reducer

时间:2018-04-01 13:50:18

标签: javascript reactjs

我有一个购物车网站和用户购物车存储在localStorage

现在,当我的页面刷新时,我想用我的localStorage初始化我的商店,但是我收到了这个错误:

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

0 个答案:

没有答案