欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React使用Redux實(shí)現(xiàn)組件通信的項(xiàng)目實(shí)踐

 更新時(shí)間:2025年09月17日 10:47:51   作者:柯南二號  
本文主要介紹了React使用Redux實(shí)現(xiàn)組件通信的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在 React 項(xiàng)目中,父子組件通信通常依賴 props,兄弟組件通信則通過 狀態(tài)提升Context。但是當(dāng)應(yīng)用逐漸復(fù)雜時(shí),這種方式會顯得繁瑣。此時(shí) Redux 就派上用場了 —— 它可以集中管理全局狀態(tài),任何組件都能方便地訂閱和修改狀態(tài)。

本文將帶你實(shí)現(xiàn)一個(gè)簡單的 計(jì)數(shù)器 demo,演示 Redux 在 React 中的組件通信。

一、環(huán)境準(zhǔn)備

安裝必要依賴:

npm install @reduxjs/toolkit react-redux

Redux Toolkit(RTK)是 Redux 官方推薦的寫法,簡化了很多冗余代碼。

二、目錄結(jié)構(gòu)

src/
  store.ts
  features/
    counter/
      counterSlice.ts
      CounterA.tsx
      CounterB.tsx
  App.tsx
  index.tsx
  hooks.ts

這里我們會寫兩個(gè)兄弟組件:CounterACounterB,通過 Redux 實(shí)現(xiàn)通信。

三、配置 Redux Store

src/store.ts

import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./features/counter/counterSlice";

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

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

四、定義 Slice(狀態(tài)邏輯)

src/features/counter/counterSlice.ts

import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../../store";

interface CounterState {
  value: number;
}

const initialState: CounterState = { value: 0 };

const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
    addBy: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    },
  },
});

export const { increment, decrement, addBy } = counterSlice.actions;
export default counterSlice.reducer;

// 選擇器
export const selectCount = (state: RootState) => state.counter.value;

五、封裝 Typed Hooks(TS推薦)

src/hooks.ts

import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import type { RootState, AppDispatch } from "./store";

export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

六、兄弟組件通信示例

CounterA.tsx

import React from "react";
import { useAppDispatch } from "../../hooks";
import { increment, addBy } from "./counterSlice";

export default function CounterA() {
  const dispatch = useAppDispatch();

  return (
    <div style={{ border: "1px solid #ccc", padding: 10, margin: 10 }}>
      <h3>我是 CounterA</h3>
      <button onClick={() => dispatch(increment())}>+1</button>
      <button onClick={() => dispatch(addBy(5))}>+5</button>
    </div>
  );
}

CounterB.tsx

import React from "react";
import { useAppSelector } from "../../hooks";
import { selectCount } from "./counterSlice";

export default function CounterB() {
  const count = useAppSelector(selectCount);

  return (
    <div style={{ border: "1px solid #ccc", padding: 10, margin: 10 }}>
      <h3>我是 CounterB</h3>
      <p>當(dāng)前計(jì)數(shù):{count}</p>
    </div>
  );
}

七、應(yīng)用入口

App.tsx

import React from "react";
import CounterA from "./features/counter/CounterA";
import CounterB from "./features/counter/CounterB";

export default function App() {
  return (
    <div style={{ padding: 20 }}>
      <h2>Redux 通信 Demo</h2>
      <CounterA />
      <CounterB />
    </div>
  );
}

index.tsx

import React from "react";
import { createRoot } from "react-dom/client";
import { Provider } from "react-redux";
import { store } from "./store";
import App from "./App";

const root = createRoot(document.getElementById("root")!);
root.render(
  <Provider store={store}>
    <App />
  </Provider>
);

八、運(yùn)行效果

  1. 點(diǎn)擊 CounterA 中的 +1+5 按鈕,會觸發(fā) Redux 的 dispatch,修改全局狀態(tài)。
  2. CounterB 會實(shí)時(shí)響應(yīng),展示最新的計(jì)數(shù)值。

?? 這就實(shí)現(xiàn)了 兄弟組件通信,而且隨著應(yīng)用規(guī)模擴(kuò)大,你只需要在需要的地方使用 Redux,不再需要復(fù)雜的 props 傳遞。

九、總結(jié)

  • 父子通信:直接用 props。
  • 兄弟通信:Redux 是一種集中化管理方式,適合跨層級或復(fù)雜場景。
  • Redux Toolkit 簡化了 reducer 和 action 的寫法,官方推薦。
  • 在更大規(guī)模應(yīng)用中,還可以結(jié)合 Redux Persist(持久化存儲)、RTK Query(數(shù)據(jù)請求管理)。

到此這篇關(guān)于React使用Redux實(shí)現(xiàn)組件通信的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)React Redux 組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • React?Fiber構(gòu)建completeWork源碼解析

    React?Fiber構(gòu)建completeWork源碼解析

    這篇文章主要為大家介紹了React?Fiber構(gòu)建completeWork源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • React 的 getDefaultProps簡介、用法與最佳實(shí)踐方案

    React 的 getDefaultProps簡介、用法與最佳實(shí)踐方案

    React組件通過getDefaultProps設(shè)置默認(rèn)屬性,防止未傳props導(dǎo)致的錯(cuò)誤,隨著React發(fā)展,方式從方法演變?yōu)閟tatic defaultProps及函數(shù)參數(shù)默認(rèn)值,建議根據(jù)項(xiàng)目選擇合適語法,本文給大家介紹React 的 getDefaultProps簡介、用法與最佳實(shí)踐方案,感興趣的朋友跟隨小編一起看看吧
    2025-09-09
  • react實(shí)現(xiàn)記錄拖動排序

    react實(shí)現(xiàn)記錄拖動排序

    這篇文章主要介紹了react實(shí)現(xiàn)記錄拖動排序的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • 詳解React中的this指向

    詳解React中的this指向

    這篇文章主要介紹了React中的this指向的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下
    2021-04-04
  • 淺談React多個(gè)setState會調(diào)用幾次

    淺談React多個(gè)setState會調(diào)用幾次

    在React的生命周期鉤子和合成事件中,多次執(zhí)行setState,會被調(diào)用幾次,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-11-11
  • react hooks實(shí)現(xiàn)原理解析

    react hooks實(shí)現(xiàn)原理解析

    這篇文章主要介紹了react hooks實(shí)現(xiàn)原理,文中給大家介紹了useState dispatch 函數(shù)如何與其使用的 Function Component 進(jìn)行綁定,節(jié)后實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • React?中的重新渲染類組件及函數(shù)組件

    React?中的重新渲染類組件及函數(shù)組件

    這篇文章主要為大家介紹了React?中的重新渲染類組件及函數(shù)組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • React使用context進(jìn)行跨級組件數(shù)據(jù)傳遞

    React使用context進(jìn)行跨級組件數(shù)據(jù)傳遞

    這篇文章給大家介紹了React使用context進(jìn)行跨級組件數(shù)據(jù)傳遞的方法步驟,文中通過代碼示例給大家介紹的非常詳細(xì),對大家學(xué)習(xí)React context組件數(shù)據(jù)傳遞有一定的幫助,感興趣的小伙伴跟著小編一起來學(xué)習(xí)吧
    2024-01-01
  • Shopee在React?Native?架構(gòu)方面的探索及發(fā)展歷程

    Shopee在React?Native?架構(gòu)方面的探索及發(fā)展歷程

    這篇文章主要介紹了Shopee在React?Native?架構(gòu)方面的探索,本文會從發(fā)展歷史、架構(gòu)模型、系統(tǒng)設(shè)計(jì)、遷移方案四個(gè)方向逐一介紹我們?nèi)绾我徊讲降貪M足多團(tuán)隊(duì)在復(fù)雜業(yè)務(wù)中的開發(fā)需求,需要的朋友可以參考下
    2022-07-07
  • ReactNative集成個(gè)推消息推送過程詳解

    ReactNative集成個(gè)推消息推送過程詳解

    這篇文章主要為大家介紹了ReactNative集成個(gè)推消息推送過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08

最新評論