React-Redux中actions

news/2024/7/15 19:17:41 标签: react.js, 前端, 前端框架

一、同步actions

1.概念

说明:在reducers的同步修改方法中添加action对象参数,在调用actionCreater的时候传递参数,数会被传递到action对象payload属性上。

2.reducers对象

说明:声明函数同时接受参数

const counterStore=createSlice({
    name:"counter",
    // 初始化状态
    initialState:{
        count:0
    },
    // 修改状态的方法 同步方法
    reducers:{
        addToNum(state,action){
            state.count+=action.payload
        }
    }
})
// 解构actionCreater函数
const {addToNum}= counterStore.actions
// 获取reducer
const reducer=counterStore.reducer

// 按需导出actionCreator
export {addToNum}

// 以默认导出的方式导出reducer
export default reducer

3.app.js

说明: 导入函数进行调用。

import { useDispatch, useSelector } from "react-redux";
import {addToNum} from "./store/modules/counterStore"
function App() {
  const {count}=useSelector(state=>state.counter)
  const dispatch=useDispatch()
  return (
    <div className="App">
  <button onClick={()=>dispatch(addToNum(10))}>add To 10</button>
    </div>
  );
}

export default App;

二、异步actions

1.channelStore.js

import {createSlice} from "@reduxjs/toolkit"
import axios from 'axios'
// 1.创建store写法保持不见,配置好同步修改状态的方法
const channelStore= createSlice({
    name:'channel',
    initialState:{
        channelList:[]
    },
    reducers:{
        setChannels(state,action){
            state.channelList=action.payload
        }
    }
})

// 异步请求
const {setChannels}=channelStore.actions

const fetchChannelList=()=>{
    return async(dispatch)=>{
       await axios.get('https://st2msh.laf.run/react_get_list').then(res=>{
        dispatch(setChannels(res.data))
       }).catch(err=>{
        console.log(err);
       })
    }
}

export {fetchChannelList}
const reducer=channelStore.reducer



export default reducer

2. index.js

import { configureStore} from "@reduxjs/toolkit"
// 导入子模块reducer
import counterReducer from "./modules/counterStore"
import channelReducer from "./modules/channelStore"


const store=configureStore({
    reducer:{
        counter:counterReducer,
        channel:channelReducer
    }
})

export default store

3.app.js

import { useDispatch, useSelector } from "react-redux";
import {inscrement,decrement,addToNum} from "./store/modules/counterStore"
import {fetchChannelList} from "./store/modules/channelStore"
import { useEffect } from "react";
function App() {
  const {count}=useSelector(state=>state.counter)
  const {channelList}=useSelector(state=>state.channel)
  const dispatch=useDispatch()

  useEffect(()=>{
    dispatch(fetchChannelList())
  },[dispatch])
  return (
    <div className="App">
            {count}
            <button onClick={()=>dispatch(inscrement())}>+</button>
            <button onClick={()=>dispatch(decrement())}>-</button>
  <button onClick={()=>dispatch(addToNum(10))}>add To 10</button>
   <ul>
    {channelList.map(item=><li key={item.id}>{item.name}</li>)}
   </ul>
   
    </div>
  );
}

export default App;

4.效果展示

 

 

 

 


http://www.niftyadmin.cn/n/5414959.html

相关文章

redis centos7 单点搭建

redis centos 安装步骤 下载源文件编译Redis拷贝编译后文件修改配置文件启动redis 下载源文件 wget https://download.redis.io/redis-stable.tar.gz编译Redis tar -xzvf redis-stable.tar.gz cd redis-stable make如果编译成功&#xff0c;你会在src目录中找到几个 Redis 二…

阿里云服务器哪个地域比较好?地域选择的影响因素与建议

阿里云服务器地域选择方法&#xff0c;如何选择速度更快、网络延迟更低的地域节点&#xff0c;地域指云服务器所在的地理位置区域&#xff0c;地域以城市划分&#xff0c;如北京、杭州、深圳及上海等&#xff0c;如何选择地域&#xff1f;建议根据用户所在地区就近选择地域&…

httprunner日志跟踪

1. 日志位置 1.1. 关键字&#xff1a;.log generate testcase log: D:\httprunner\demo\logs\082dd63e-56b9-40c1-9392-039dc0a03667.run.log 1.2. 控制台效果 2. 项目位置 3. 日志输出原理 3.1. 测试用例ID和Request ID 为排除故障&#xff0c;每个测试用例将生成唯一ID&…

社区店选址标准:如何选择适合你业务的理想位置

选址是实体店成功的关键因素之一&#xff0c;而社区店更是要紧密结合社区的特点来选择。 作为一名鲜奶吧开店5年的创业者&#xff0c;我将为大家分享一些实用的社区店选址标准。 1、社区类型&#xff1a; 首先要明确你的目标客户群体&#xff0c;然后选择与之匹配的社区类型…

《汇编语言》- 读书笔记 - 第17章-实验17 编写包含多个功能子程序的中断例程

《汇编语言》- 读书笔记 - 第17章-实验17 编写包含多个功能子程序的中断例程 逻辑扇区根据逻辑扇区号算出物理编号中断例程&#xff1a;通过逻辑扇区号对软盘进行读写 代码安装 int 7ch 测试程序效果 实现通过逻辑扇区号对软盘进行读写 逻辑扇区 计算公式: 逻辑扇区号 (面号*8…

nVisual+AI实现综合布线智能化运维管理

传统的综合布线系统依据TIA-606规范在配线架、跳线、面板上都粘贴了标签&#xff0c;标签作为一个综合布线项目中元器件的唯一标识&#xff0c;对综合布线日常运维管理过程中查询连接关系、定位设备位置至关重要&#xff0c;但标签所能记录的信息毕竟有限&#xff0c;因此可视化…

ADS功分器模型含义

ADS功分器模型含义 文章目录 ADS功分器模型含义dbpolar和单个值polar和单个值polar和dbpolar单个值 S21和S31传输系数 S11和S22反射系数 Isolation 隔离度 Zref 端口的参考阻抗&#xff0c;默认为50Ω CheckPassivity 检查是否无源&#xff0c;默认是无源器件 目前根据仿真结…

c++ set集合按位置(索引)查询元素的方法

在STL中集合好处是可以对输入的元素进行排序&#xff0c;就是说在某些情况下对某些需要有序的元素可以使用set集合存储。不过在set集合没有提供按值查询的方法。 在集合中&#xff0c;set集合不允许出现重复元素&#xff0c;使用multiset集合可以存储重复元素。 按位置查询 按…