当前位置: 首页 > news >正文

学习react第三天

Redux

类似vuex和pinia

先安装对应包和插件

npm install @reduxjs/toolkit react-redux

使用步骤

1.配置子模块

相关参数:createSlice(创建仓库),name,initialState,reducers,counterStore.actions,counterStore.reducer

import { createSlice } from "@reduxjs/toolkit"const counterStore = createSlice({// 名称name: "counterStore",// 初始状态数据initialState: {count: 0,   },// 修改数据方法reducers: {increment(state) {state.count += 1},decrement(state) {state.count -= 1}},
})// 从counterStore中解构出counterCreater方法导出
export const { increment, decrement } = counterStore.actions
// 获取counterStore的reducer导出
const counterReducer = counterStore.reducer
export default counterReducer

2.导入根store组合

相关参数:configureStore(合并子仓库)

import counterReducer from "./modules/counterStore";
import { configureStore } from "@reduxjs/toolkit";
import channelReducer from "./modules/channelStore";const store = configureStore({reducer: {counterStore: counterReducer,channelStore: channelReducer,},
}); export default store;

3.在全局注册

相关参数:Provider

import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import store from './store/index.js'
import { Provider } from 'react-redux'createRoot(document.getElementById('root')).render(<StrictMode>{/* 注入redux */}<Provider store={store}><App /></Provider></StrictMode>,
)

4.在组件中获取store中的数据和修改数据的方法

相关参数:useDispatch(获取dispatch), useSelector(获取对应store中的数据)

import { useDispatch, useSelector } from 'react-redux'
import { increment, decrement } from '@/store/modules/counterStore.js'
import './App.css'function App() {const { count } = useSelector(state => state.counterStore)const dispatch = useDispatch()return (<div className="App"><h1>Count: {count}</h1><button onClick={() => dispatch(increment())}>count+</button><button onClick={() => dispatch(decrement())}>count-</button></div>)
}export default App

5.传参方法

相关参数:action.payload

addToNumber(state, action) {// action.payload 是传递过来的参数state.count += action.payload
}

6.异步传参

定义异步请求函数来dispatch reducers来修改数据

// 异步请求
const getChannelList = () => {return async (dispatch) => {const res = await axios.get('http://geek.itheima.net/v1_0/channels')dispatch(setChannelList(res.data.data.channels))}
}

在组件页面调用的时候,要在useEffect钩子里面调用

  useEffect(() => {dispatch(getChannelList())}, [dispatch])

http://www.dtcms.com/a/604869.html

相关文章:

  • 营销软文网站西安网站建设网络公司熊掌号
  • 二分查找算法介绍及使用
  • [element-plus] el-tree 动态增加节点,删除节点
  • SQL:从数据基石到安全前线的双重审视
  • 数据结构:双向链表(1)
  • 【C++】深入拆解二叉搜索树:从递归与非递归双视角,彻底掌握STL容器的基石
  • 深圳趣网站建设网络外包服务公司
  • Axios 全面详解
  • ios-AVIF
  • 360网站建设公司哪家好石家庄有哪些互联网公司
  • 单机并发简介
  • 自相关实操流程
  • java基础-集合接口(Collection)
  • 基于中国深圳无桩共享单车数据的出行目的推断与时空活动模式挖掘
  • 【Rust】通过系统编程语言获取当前系统内存、CPU等运行情况,以及简单实现图片采集并设置系统壁纸
  • 【计算思维】蓝桥杯STEMA 科技素养考试真题及解析 D
  • 智能合同系统,如何为企业合同管理保驾护航?
  • 基于Rust实现爬取 GitHub Trending 热门仓库
  • 深圳市建设局官方网站曼联对利物浦新闻
  • 【Android 组件】实现数据对象的 Parcelable 序列化
  • CrowdDiff: 使用扩散模型进行多假设人群密度估计
  • 同创企业网站源码wordpress自定义简单注册
  • 在 Android ARM64 上运行 x86_64 程序
  • 幽冥大陆(二十)屏幕录像特效增加节目效果——东方仙盟炼气期
  • 类加载机制、生命周期、类加载器层次、JVM的类加载方式
  • 数据智能开发五 技术架构
  • 免费的app软件下载网站个人网站备案 法律说明
  • MFC Check Box控件完全指南:属性设置、样式定制与高级应用
  • 广州 网站 建设支付网站建设费入什么科目
  • 西宁做网站需要多少钱wordpress怎么安装模板