簡體   English   中英

React 組件沒有得到由 reducer 設置的 redux 的初始 state

[英]React component not getting redux's initial state set by reducer

在創建減速器時,我設置了初始 state,但在第一次加載組件時,我得到一個空的 state。

 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 ) )

我得到了解決方案。 實際上我之前沒有使用組合減速器,這就是為什么減速器的 state 沒有被初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM