在我的项目中,我试图合并两个化简器,但是每当我尝试使用CombineReducers({})合并它们时,我的道具就变得不确定,并且我的状态(来自store.getState())变成两个对象名称我的减速器。查看我的减速机设置
root / reducers.js
import { combineReducers } from 'redux'
import dashboardReducer from '../views/DashboardPage/redux/reducers'
import formReducer from '../views/FormPage/redux/reducers'
export default combineReducers({
dashboardReducer,
formReducer
})
configureStore.js
import { createStore, compose } from "redux";
import rootReducer from "./reducers";
const storeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(rootReducer);
export default store;
dashboard / reducer.js
import {ADD_FIELD} from "./types"
const initialState = {
fields: [{title: "bla", text: "jorge", id: 1}],
};
const dashboardReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_FIELD:
return {
...state,
fields: state.fields.concat(action.payload)
};
default:
return state;
}
};
export default dashboardReducer;
forms / reducer.js
const formReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state
}
};
export default formReducer;
分别在“ store.getState()”和“ this.props”(在将状态映射到props之后)上调用console.log分别返回以下内容:
console.log
的
如果有关系,我正在使用react-router
答案 0 :(得分:1)
这是正确的行为,听起来您需要修改mapStateToProps
才能正确处理它,因此您可能希望将映射功能添加到问题中。
export default combineReducers({
dashboardReducer,
formReducer
})
使用combineReducers
意味着您将使用不同的reducer管理状态的不同部分,这些部分将根据您提供的对象中的键进行命名。您可能希望将其更改为:
import dashboard from '../views/DashboardPage/redux/reducers'
import form from '../views/FormPage/redux/reducers'
export default combineReducers({
dashboard,
form
})
这将导致您的状态具有以下形状:
{
dashboard: { dash: "things" },
form: {}
}
您的仪表板简化程序将在状态设置为
时被调用{ dash: "things" }
,您的mapStateToProps
将需要相应地读取状态
return {
fields: state.dashboard.fields
};