React:状态管理Redux

16 篇文章 0 订阅
3 篇文章 0 订阅
参考资源

redux中文网

一、介绍

Redux 是 JavaScript 应用的状态容器,提供可预测的状态管理。
Redux Toolkit 是官方推荐的编写 Redux 逻辑的方法。它围绕 Redux 核心,并包含我们认为对于构建 Redux 应用必不可少的软件包和功能。Redux Toolkit 建立在我们建议的最佳实践中,简化了大多数 Redux 任务,防止了常见错误,并使编写 Redux 应用程序更加容易。

基本流程:
redux逻辑

二、安装依赖

npm i @reduxjs/toolkit react-redux

三、上代码

1. 最终代码目录

在这里插入图片描述

2.先创建 store 目录

2.1 store/modules/counterStore.js

import { createSlice } from "@reduxjs/toolkit";

const counterStore = createSlice({
    // 1. name:用作生成的 action type 的前缀
    name: 'counter',
    // 2. initialState:reducer的初始state
    initialState: {
        count: 0
    },
    // 3. reducers:对象,键是字符串,值是处理特定 actions 的 “case reducer” 函数
    reducers: {
        increment(state) {
            state.count ++;
        },
        decrement(state) {
            state.count --;
        }
    }
})

// 结构出创建 action 对象的函数 (actionCreater)
const {increment, decrement} = counterStore.actions;
// 创建 reducer 函数
const counterReducer = counterStore.reducer;

// 导出 创建 action对象的 函数和reducer函数
export {increment, decrement};

export default counterReducer;

2.2 store/index.js

// 通过 configureStore 创建根store组合子模块
import { configureStore } from "@reduxjs/toolkit";

// 导入 counter 的 reducer
import counterReducer from "./modules/counterStore";

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

export default store;
3. index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
// 引入 store
import store from './store';
// react-redux 中的 Provider 注入 store
import { Provider } from 'react-redux';

import App from "./App";


const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(
  <React.StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>
);

4. App.jsx
import { useSelector, useDispatch } from "react-redux";
import {increment, decrement} from './store/modules/counterStore'


function App() {
  // 1. 在组件中通过 useSelector 获取store中的数据
  // state.counter 的 counter 对应 `store/index.js` 中 reducer下的 counter
  const {count} = useSelector(state => state.counter);
  // 2. 在组件中通过 useDispatch 生成提交 action 对象的dispatch函数
  const dispatch = useDispatch()

    return (
      <>
        <button onClick={() => dispatch(increment())}>+</button>
        <span>{count}</span>
        <button onClick={() => dispatch(decrement())}>-</button>
      </>
    )
}

export default App;

四、结果

在这里插入图片描述

五、进阶

1. 提交action 时传参

store的reducers方法中除了 state,还有个 action 参数,通过 action.payload 可以获得传入的参数:

1.1 修改store/modules/counterStore.js,在reducers中新增addToNum:

...
// 3. reducers:对象,键是字符串,值是处理特定 actions 的 “case reducer” 函数
reducers: {
    increment(state) {
        state.count ++;
    },
    decrement(state) {
        state.count --;
    },
    // 此处为新增
    addToNum(state, action) {
        state.count += action.payload; // payload 为固定属性,用来传参
    }
}
...

// 解构并导出新增的 addToNum

// 解构出创建 action 对象的函数 (actionCreater)
const {increment, decrement, addToNum} = counterStore.actions;
...
// 导出 创建 action对象的 函数和reducer函数
export {increment, decrement, addToNum};

1.2 在App.jsx 新增 +10、+20 两个按钮

import {increment, decrement, addToNum} from './store/modules/counterStore'

...
function App() {
  ...
    return (
      <>
        <button onClick={() => dispatch(increment())}>+</button>
        <span>{count}</span>
        <button onClick={() => dispatch(decrement())}>-</button>
        {/* 按传入的参数控制增加 */}
        <button onClick={() => dispatch(addToNum(10))}>+10</button>
        <button onClick={() => dispatch(addToNum(20))}>+20</button>
      </>
    )
}

1.3 效果:
在这里插入图片描述

2. 异步操作

2.1 新建store/modules/channelStore.js文件,

  • 创建store的方法保持不变,配置好同步修改状态的方法
  • 单独封装一个函数,在函数内部 return 一个新函数,该新函数中:
    1. 封装异步请求获取数据
    1. 调用同步 actionCreater 传入异步数据,生成一个action对象,并使用dispatch提交
    1. 组件中 dispatch 的写法保持不变
import { createSlice } from '@reduxjs/toolkit';
import axios from 'axios'

const channelStore = createSlice({
    name: 'channel',
    initialState: {
        channelList: []
    },
    reducers: {
        setChannelList(state, action) {
            state.channelList = action.payload
        }
    }
})

const {setChannelList} = channelStore.actions;

const channelReducer = channelStore.reducer

// 异步请求部分: 1. 新建一个函数
const fetchChannelList = () => {
	// 2. return 一个函数
    return async (dispatch) => {
    	// 3. 发送异步请求获取数据
        const res = await axios.get('http://geek.itheima.net/v1_0/channels');
		// 4. dispatch 提交同步 setChannelList 函数
        dispatch(setChannelList(res.data.data.channels))
    }
}

// 5.导出异步逻辑函数
export {fetchChannelList}
export default channelReducer;

2.2 store/index.js

...
import channelReducer from "./modules/channelStore";

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

2.3 App.jsx组件中在 useEffect 中调用异步操作函数

import { fetchChannelList } from "./store/modules/channelStore";
...
const {channelList} = useSelector(state => state.channel);

 useEffect(() => {
    dispatch(fetchChannelList())
  }, [dispatch])
...
function App() {
	return (
		...
		<ul>
		   {
		     channelList.map(item => <li key={item.id}>{item.name}</li>)
		   }
		</ul>
		...
	)
	

2.4 结果
在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
前端开发中的React Redux全家桶是一套常用的技术栈,用于构建复杂的Web应用程序。React是一个由Facebook开发的JavaScript库,用于构建用户界面。它通过将应用程序拆分成可重用的组件,使开发人员能够更轻松地开发、测试和维护Web应用程序。 Redux是一个用于管理应用程序状态的库。它采用了一种称为单一状态树的模式,将整个应用程序的状态存储在一个对象中,并使用纯粹的函数来修改状态Redux的核心概念包括:store、reducer和action。Store是应用程序的状态容器,reducer是一个纯函数,用于根据action来修改状态,而action代表用户触发的操作。 React Redux是将ReactRedux结合在一起使用的库。通过使用React Redux,我们可以将Redux状态管理功能集成到React组件中。React Redux提供了一种称为容器组件的机制,它负责从Redux store中提取数据,并将其作为props传递给展示组件。这种分离开发的模式使得代码更加模块化和易于维护。 React Redux全家桶还包括一些其他的辅助库,如React Router用于跟踪和管理应用程序的URL路径,以及Redux Thunk或Redux Saga用于处理异步操作。这些库的整合和使用能够帮助开发人员构建可扩展、高效和易于维护的前端应用程序。 总之,前端开发中的React Redux全家桶提供了一套完善的工具和库,帮助开发人员构建复杂的Web应用程序。它能够将状态管理和用户界面开发结合在一起,并提供了一种模块化和分离开发的解决方案。通过学习和使用React Redux全家桶,开发人员可以提高开发效率,并构建出更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端卡卡西呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值