1.首先在header–>store下新建文件actionCreators.js
2.actionCreators.js是将action的类型拆分出来,代码如下:
export const searchFocus = () => ({
type:'search_focus'
});
export const searchBlur = () => ({
type:'search_blur'
});
3.在header目录下的index.js文件中引入 actionCreators
import * as actionCreators from './store/actionCreators';
4.将派发action的代码段进行修改
在这里就成功的将action拆分出去。
5.在header目录下的store中新建一个文件actionTypes.js
6. 在actionTypes.js中我们将action的类型字符型变量名替换为一个变量
export const SEARCH_FOCUS = 'header/search_focus';
export const SEARCH_BLUR = 'header/search_blur';
- 然后我们在actionCreators.js引入actionTypes,并将字符变量名替换为大写的变量
import * as actionTypes from './actionTypes';
export const searchFocus = () => ({
type:actionTypes.SEARCH_FOCUS
});
export const searchBlur = () => ({
type:actionTypes.SEARCH_BLUR
});
- 因为reducer中也引入了action的类型,所以需要引入actionTypes,并且将字符变量名更改为现在的变量