React的Redux的状态管理

步骤

1.创建新项目

复制代码
npx create-react-app react-redux

2.安装配套工具

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

3.启动项目

复制代码
npm run start

4.在src目录下创建store文件夹

5.在store文件夹下创建modules文件夹

6.在store文件夹里创建index.js文件

7.在counterStore.js文件里编写子store(使用React Toolkit 创建 counterStore)

复制代码
// createSlice  是为了创建store用的
import { createSlice} from "@reduxjs/toolkit"


const counterStore= createSlice({
    // 模块
    name:'counter',
    // 初始化state(状态)
    initialState:{
        count:0
    },
    //(修改状态的方法) 编写书写数据的方法,同步方法,支持直接修改
    reducers:{
        inscrement(state){
            state.count++;
        },
        decrement(state){
            state.count--;
        },
        inscrementTen(state,actions){
            console.log("actions",actions);
            state.count= state.count+actions.payload;
        },
        // action传递对象
        actionObg(state,actions){
            console.log("测试传递对象",actions);
        }
    }
})
// 解构出来actionCreater函数
const {inscrement,decrement,inscrementTen,actionObg} =counterStore.actions;
// 获取reducer
const counterReducer = counterStore.reducer
// 以按需导出的方式导出actionCreater
export {inscrement,decrement,inscrementTen,actionObg}
// 以默认导出的方式发哦出reducer
export default counterReducer;

8.在store文件夹的index.js里组合moudels里的子模块,并导出store

复制代码
import { configureStore } from "@reduxjs/toolkit";
// 导入子模块reducer (counterReducer,channelReducer  这俩名称是counterStore.js和chaenlStore.js最后一行导出来的名称)
import counterReducer from './modules/counterStore'
import channelReducer from "./modules/chaenlStore";

// 创建子组合模块
// 根store
const store =configureStore({
    reducer:{
        counter:counterReducer,
        channel:channelReducer
    }
})
export default store 

9.在src文件夹下的index.js文件里 为React注入store

复制代码
// 使React
import store from './store';
// 导出;来的Provider  用于下面标签里
import {Provider} from 'react-redux'
const root = ReactDOM.createRoot(document.getElementById('root'));


root.render(
  <React.StrictMode>
    {/* 注入store   Provider标签很重要 */}
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>
);
10.React组件使用store中的数据

在React组件中使用store中的数据,需要用到一个钩子函数-useSelector,它的作用是把store中的数据映射到组件中

useDispatch是React-Redux库提供的一个钩子函数,它用于访问Redux store的dispatch函数。useDispatch可以让你从函数组件中派发actions。

app.js文件

复制代码
// useSelector  获取store里面 变量
// useDispatch作用可以修改store里面变量
import {useSelector,useDispatch} from 'react-redux'
// 导入actionCreater
import { decrement,inscrement,inscrementTen,actionObg } from './store/modules/counterStore'; 


function App() {
  // 使用回调函数state拿到任意一个模块  counter和channel  名称来自strore文件夹下的index.js文件里绑定的模块名
  const {count} =useSelector(state => state.counter)
  const {channelList}= useSelector(state=>state.channel)
  const dispatch=useDispatch();
  // 使用useEffect触发异步接口调用   [dispatch]  的意思是调用dispatch一次执行一次
  useEffect(()=>{
    dispatch(fetchChannlList());
  },[dispatch])

  return (
    <div className="App"> 
    <button onClick={()=>dispatch(decrement())}>-</button>
      alksdfn---{count}
    <button onClick={()=>dispatch(inscrement())}>+</button>
    <button onClick={()=>dispatch(inscrementTen(10))}>+10</button>
    <button onClick={()=>dispatch(actionObg({'age':10,'qie':20}))}>提交action传递对象</button>

    <ul>
        {channelList.map((item,index)=><li key={item.id}>{item.name}</li>)}
      <li></li>
    </ul>
    </div>
  );
}

export default App;

修改Store里面变量的唯一方法就是提交一个action

redux异步从后台获取数据

安装Axios异步请求库、

复制代码
npm i axios

谷歌插件调试React项目

插件下载地址(https://chromewebstore.google.com/detail/lmhkpmbekcpmknklioeibfkpmmfibljd

相关推荐
A923A14 小时前
【从零开始学 React | 第四章】useEffect和自定义Hook
前端·react.js·fetch·useeffect
ZC跨境爬虫14 小时前
批量爬取小说章节并优化排版(附完整可运行脚本)
前端·爬虫·python·自动化
ZC跨境爬虫14 小时前
海南大学交友平台登录页开发实战day4(解决python传输并读取登录信息的问题)
开发语言·前端·python·flask·html
来一颗砂糖橘14 小时前
pnpm:现代前端开发的高效包管理器
前端·pnpm
前端摸鱼匠14 小时前
Vue 3 的defineProps编译器宏:详解<script setup>中defineProps的使用
前端·javascript·vue.js·前端框架·ecmascript
木斯佳14 小时前
前端八股文面经大全: 美团财务科技前端一面 (2026-04-09)·面经深度解析
前端·实习面经·前端初级
天外天-亮14 小时前
Vue2.0 + jsmind:开发思维导图
javascript·vue.js·jsmind
LIO14 小时前
React 零基础入门,一篇搞懂核心用法(适合新手)
前端·react.js
TeamDev14 小时前
JxBrowser 8.18.2 版本发布啦!
java·前端·跨平台·桌面应用·web ui·jxbrowser·浏览器控件
netkiller-BG7NYT14 小时前
yoloutils - Openclaw Agent Skill
前端·webpack·node.js