学习react第三天

Redux

类似vuex和pinia

先安装对应包和插件

javascript 复制代码
npm install @reduxjs/toolkit react-redux

使用步骤

1.配置子模块

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

javascript 复制代码
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(合并子仓库)

javascript 复制代码
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

javascript 复制代码
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中的数据)

javascript 复制代码
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

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

6.异步传参

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

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

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

javascript 复制代码
  useEffect(() => {
    dispatch(getChannelList())
  }, [dispatch])
相关推荐
非凡ghost11 小时前
AI修图不“造假“,摄影师的出片效率加速器
服务器·前端·人工智能·电脑
2601_9516152011 小时前
网页设计模板源码 web前端模板网页源码下载
前端
mmsx11 小时前
osmdroid 地图实战 03|地图的"分层世界":离线方案、图层模型与 Overlay 体系
android·前端
郭邯11 小时前
从零到一:我用 AI 写了个复利计算器,顺便治好了我的"公式恐惧症"
前端
Pointer Pursuit11 小时前
C++11特性(二)
前端·c++·算法
天道kabuto12 小时前
Vue3 升级踩坑:子组件 click 事件为什么会触发两次?
前端
Nayana12 小时前
《Web 到 HarmonyOS》-- Ability、Stage、模块化与路由导航
前端
ShineWinsu12 小时前
对于 Vue 3:从为什么学 Vue,到声明式渲染与数据响应式的解析
前端·javascript·vue.js
粥里有勺糖12 小时前
视野修炼-技术周刊第132期 | 一些有趣的组件
前端·github·aigc
এ慕ོ冬℘゜12 小时前
样式控制 .css ()
前端·css