【问题标题】:React component not getting redux's initial state set by reducerReact 组件没有得到 reducer 设置的 redux 初始状态
【发布时间】:2021-06-04 21:16:12
【问题描述】:

在创建 reducer 时,我已经设置了初始状态,但是在第一次加载组件时,我得到了一个空状态。

reducer file -
    const initialValues = { geo: { latitude: 0, longitude: 0 } }
    const emp = (state = initialValues, action) => {

    switch (action.type) {
        case 'bike': {
            return { ...state, data: action.payload }

        }
        case 'car': {
            return { ...state, headers: action.payload }

        }
        default:
            return state

    }
}

component file- 
export default function Emp() {

    const emp = useSelector(state => state)
   
    console.log('emp state', emp) 
}

index.js

const store = createStore(reducer,
  {},
  compose(
    applyMiddleware(
      loggingMiddleware,
    ),
    window.devToolsExtension ? window.devToolsExtension() : f => f
  )
)

【问题讨论】:

    标签: javascript reactjs redux ecmascript-6 react-redux


    【解决方案1】:

    我得到了解决方案。实际上,在这之前我没有使用 combine reducer,这就是为什么 reducer 的状态没有被初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 2021-03-10
      相关资源
      最近更新 更多