reactjs - redux combineReducers() 在我的 Ducks 模式项目中不起作用
问题描述
我想分离模块,所以我试图分离目录中的src/store/modules
文件。
为了合并reducer模块,我使用combineReducers()
in modules/index.js
。
在分离这些模块之前,modules/index.js
文件的代码是modules/board.js
.
然后我添加了board.js文件。我将index.js的代码移至board.js。最后我添加combineReducer()
了index.js,但不知何故它不起作用。
src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './containers/App';
import store from './store';
const rootElement = document.getElementById('root');
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
rootElement
);
src/containers/BoardContainer.js
import React from 'react';
import Board from '../components/Board';
import { connect } from 'react-redux';
import * as boardActions from '../store/modules/board';
class BoardContainer extends React.Component {
componentWillMount() {
this.props.handleReadBoards();
}
render() {
/* ... */
}
}
const mapStateToProps = (state) => {
return {
boardList: state.get('boardList')
};
}
const mapDispatchToProps = (dispatch) => {
return {
handleReadBoards: () => { dispatch(boardActions.readBoardList()) }
};
}
export default connect(mapStateToProps, mapDispatchToProps)(BoardContainer);
src/store/index.js
// redux
import { createStore, applyMiddleware, compose } from 'redux';
import reducers from './modules';
// redux middleware
import thunk from 'redux-thunk';
const store = createStore(reducers,
compose(applyMiddleware(thunk))
);
export default store;
src/store/modules/index.js
import { combineReducers } from 'redux';
import board from './board';
export default combineReducers({
board
});
src/store/modules/board.js
import { createAction, handleActions } from 'redux-actions';
import { Map, List } from 'immutable';
import * as boardApi from '../../lib/api/board';
// Action Types
const READ_BOARD_LIST = 'board/READ_BOARD_LIST';
// Action Creators
export const readBoardList = () => async (dispatch) => {
try {
const boardList = await boardApi.getBoardList();
dispatch({
type: READ_BOARD_LIST,
payload: boardList
});
} catch (err) {
console.log(err);
}
}
// initial state
const initialState = Map({
boardList: List()
})
// reducer
// export default handleActions({
// [READ_BOARD_LIST]: (state, action) => {
// const boardList = state.get('boardList');
// return state.set('boardList', action.payload.data);
// },
// }, initialState);
// reducer
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case READ_BOARD_LIST:
return state.set('boardList', action.payload.data);
default:
return state;
}
}
解决方案
您的减速器现在包含子模块。这样您就必须说明要从哪个模块获取数据:state.board.get('boardList')
.
您可以尝试设置 redux 工具来轻松地在 redux 中可视化您的数据。
const mapStateToProps = (state) => {
return {
boardList: state.board.get('boardList')
};
}
推荐阅读
- unity3d - Unity3D 2020 - VSCode - 智能感知发生了什么?
- firebase - Firebase 使用颤振读取文档计数
- file - 显示目录下的所有文件
- django - Django 表单:如何通过实例获得第二个用户?
- c++ - `GLIBCXX_3.4.15' 未找到,但它在那里
- java - 如何在Java中将文件加载到数组中
- reactjs - 如何在子组件和父组件上使用 onClick 属性?
- elasticsearch - 基于前缀和自定义分词器的 Elasticsearch 自动建议
- javascript - 仅显示与不同数组中的所有值匹配的项目列表
- r - 错误:`summarise()` 输入 `wt` 的问题:geom_net 函数与新的 dplyr 版本不兼容