我目前使用一個減速器創建了我的商店,其中初始狀態已傳遞給它。如何將初始狀態傳遞給減速器
import reducers from './reducers'
const store = createStore(
reducers,
initialState,
compose(...enhancers)
)
// reducers.js
export default function reducer(state, action) {
console.log('state', state)
switch (action.type) {
case 'LOAD_UI':
return Object.assign({}, state, {
loadUI: action.loaded
});
case 'NO_MATCH':
return Object.assign({}, state, {
loadUI: true,
isNoMatch: action.isNoMatch
});
default:
return state;
}
}
當我登錄的狀態在我的減速功能,我得到的是設置狀態時,配置我的店:
// if we are in production mode, we get the initial state from the window object, otherwise, when we are in dev env we get it from a static file
const preloadedState = window.__INITIAL_STATE__ === '{{__PRELOADEDSTATE__}}' ? initialState : window.__INITIAL_STATE__
const store = configureStore(preloadedState)
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
, document.getElementById('root')
)
現在我想用,因爲我想包括另一個減速阿波羅在我的項目中。據http://dev.apollodata.com/react/redux.html我需要combineReducers:即
combineReducers({
todos: todoReducer,
users: userReducer,
apollo: client.reducer(),
}),
所以我需要做財產以後這樣的:
const store = createStore(
combineReducers({
apollo: client.reducer(),
reducer: reducers
}),
initialState,
compose(...enhancers)
)
但後來我減速功能不再訪問狀態(這是不確定的)。我如何確保在使用combineReducers時能夠通過這個過程?
解決方案:
按照丹尼爾·里爾登的建議,我需要我的減速器鑰匙匹配到我的初始狀態。
我的初始化狀態看起來像:
{
"pages": [
{
"id": 5,
"title": "About",
"slug": "about",
"path": "/about",
"template": "about",
"published": "2017-07-10 02:02:30",
"image": null,
"seo": {
"title": null,
"description": null,
"image": null,
"fbAdmins": null,
"gtm": null,
"schema": ""
},
"sections": [
]
},
{
"id": 10,
"title": "Contact",
"slug": "contact",
"path": "/contact",
"template": "contact",
"published": "2017-07-11 04:27:30",
"image": null,
"seo": {
"title": null,
"description": null,
"image": null,
"fbAdmins": null,
"gtm": null,
"schema": ""
},
"sections": []
},
{
"id": 4,
"title": "Home",
"slug": "home",
"path": "/",
"template": "home",
"published": "2017-07-10 01:39:48",
"image": null,
"seo": {
"title": null,
"description": null,
"image": null,
"fbAdmins": null,
"gtm": null,
"schema": ""
},
"sections": []
}
],
"services": [],
"clients": [],
"people": [],
"offices": [],
"articles": [],
"menus": {
"header": [
{
"title": "Home",
"slug": "/"
},
{
"title": "About",
"slug": "/about"
},
{
"title": "Contact",
"slug": "/contact"
}
]
},
"settings": {
"site": {
"title": null,
"siteUrl": ""
},
"logo": [],
"secondarylogo": [],
"favicon": [],
"disclaimer": null,
"tagline": null,
"social": null,
"email": null,
"phone": null,
"facebookAppId": "",
"seo": {
"title": null,
"description": null,
"image": null,
"fbAdmins": null,
"gtm": null,
"schema": ""
},
"newsLetterSignUp": {
"image": "",
"title": "",
"content": ""
},
"menu_settings": null
}
}
所以我減速看起來是這樣的:
import { combineReducers } from 'redux'
import { ApolloClient } from 'react-apollo';
const client = new ApolloClient();
import articles from './articles'
import pages from './pages'
import services from './services'
import clients from './clients'
import people from './people'
import offices from './offices'
import menus from './menus'
import settings from './settings'
export default combineReducers({
apollo: client.reducer(),
articles,
pages,
services,
clients,
people,
offices,
menus,
settings
})
所以我pages
減速只獲得頁面切開我的初始化狀態的。
謝謝。這值得我們讀一下'combineReducers'是如何工作的。我錯過了每個reducer映射到預加載狀態下的鍵的事實。 – Stretch0
作爲該文檔的作者,很高興知道它有幫助! – markerikson