08.拆分action

博客介绍了拆分action类型的代码操作。先在header–>store下新建actionCreators.js拆分action类型,在header/index.js中引入并修改派发action代码;接着在header/store中新建actionTypes.js,将action类型字符变量名替换为变量,在actionCreators.js和reducer中引入并替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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';

  1. 然后我们在actionCreators.js引入actionTypes,并将字符变量名替换为大写的变量
import * as actionTypes from './actionTypes';
export const searchFocus = () => ({
    type:actionTypes.SEARCH_FOCUS
});
export const searchBlur = () => ({
    type:actionTypes.SEARCH_BLUR
});

  1. 因为reducer中也引入了action的类型,所以需要引入actionTypes,并且将字符变量名更改为现在的变量

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值