action
组件内 dispatch之前直接dispatch({data:xxx,type:xxx}),现在改为dispatch(ACTION)

constant.js: 定义 reducer 和 action中的 type

reducer.js:
action.js:

异步的 action
方法一: 组件内写异步

方法二:store中写
npm install redux-thunk
store.js:

count_action.js:

count.jsx: 组件内调用:

本文介绍了在Redux中从直接在组件内dispatch数据和类型,转变为使用ACTION Creator的过程。通过ACTION Creator,可以更好地组织和管理异步操作。在constant.js中定义了reducer和ACTION的type,在reducer.js和action.js中分别处理ACTION。组件内部可以通过两种方式实现异步ACTION:一是直接在组件内编写,二是利用redux-thunk中间件在store.js中处理。在count.jsx组件中,展示了如何调用ACTION Creator进行异步操作。
组件内 dispatch之前直接dispatch({data:xxx,type:xxx}),现在改为dispatch(ACTION)

constant.js: 定义 reducer 和 action中的 type

reducer.js:
action.js:

方法一: 组件内写异步

方法二:store中写
npm install redux-thunk
store.js:

count_action.js:

count.jsx: 组件内调用:


被折叠的 条评论
为什么被折叠?