redux - 使用 applyMiddleware,调度不起作用
问题描述
您好,我是 redux 的新手,我没有看到问题,我查看了其他 stackoverflow,有类似的问题,但没有找到我的问题。问题是调度不起作用。
src/redux/index.js
export default combineReducers({
page: pageReducer,
fetch: fetchReducer
})
src/index.js
import allReducer from './reducers'
import { pageMd1 } from './reducers/middleware/page'
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { Provider } from 'react-redux'
const store = createStore(
allReducer,
applyMiddleware(thunk)
)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
src/redux/动作
export const PREVIOUS_PAGE = 'PREVIOUS_PAGE'
export const previous_page = (max) => {
return {
type: PREVIOUS_PAGE,
payload: max
}
}
src/redux/中间件
import { PREVIOUS_PAGE, previous_page } from '../actions'
export const update_page = ({dispatch}) => next => action => {
next(action)
if (action.type === PREVIOUS_PAGE) {
dispatch(previous_page({max: action.payload}))
}
}
export const pageMd1 = [ update_page ]
src/redux/reducer
import { NEXT_PAGE, PREVIOUS_PAGE } from './actions'
export default (state = 1, action) => {
console.log('reducer')
switch (action.type) {
case NEXT_PAGE:
return state + 1
case PREVIOUS_PAGE:
return state - 1
default:
return state
}
}
源/首页
<ChangePage max={this.state.max_pages} {...{prevPage, nextPage}} />
const mapStateToProps = state => ({
page: state.page
})
const mapDispatchToProps = (dispatch) => ({
prevPage: () => dispatch(previous_page),
nextPage: () => dispatch(next_page),
})
export default connect(mapStateToProps, mapDispatchToProps)(Home)
源/更改页面
<button className='SelectionOff' id='decrement' onClick={() => this.props.prevPage(max_page) }>Previous</button>
希望很明确......问题是调度不起作用
解决方案
你需要调用里面的函数dispatch
:
const mapDispatchToProps = (dispatch) => ({
prevPage: () => dispatch(previous_page()),
nextPage: () => dispatch(next_page()),
})
推荐阅读
- machine-learning - 验证集在机器学习研究论文中是否必不可少?
- apache-camel - 在 Apache Camel 中将对象分解为 POJO 的最佳方法
- python - 将 Parquet 文件分区列存储在不同的文件中
- api - 如何通过 OAuth 的 accessToken 通过 CData ODBC 驱动程序对 Salesforce 进行身份验证?
- python - 将从json请求接收到的数据传递给python flask socketio中的另一个函数
- php - PHP - 消息:未定义索引但索引存在
- mysql - 如何在 MySql 中的多行插入中获取自动增量 PK
- laravel - 响应式文件管理器和 TinyMCE
- c++ - 使用类静态常量整数初始化二维数组
- amazon-ec2 - 无法验证回调 URL 或验证令牌。请验证提供的信息或稍后重试