Redux中间件的正确安排

huangapple go评论57阅读模式
英文:

Correct arrangement of redux middleware

问题

以下是翻译好的内容:

假设我有代表我的React系统两个不同方面的特征X和特征Y。每个特征都有自己的Redux状态切片。在以下示例中,如果X发生更改,Y应相应地调整自己的状态。

项目结构如下:

src/features/feature-X/  {特征X的组件/状态切片}
src/features/feature-Y/  {特征Y的组件/状态切片}
src/features/base/...等等

我应该:

  • 使用thunk来调用X和Y的各自reducers,以便X和Y不耦合?(如果是这样,我应该把这个thunk放在X、Y还是其他地方?)
  • 或者,我应该在X上添加一个createListenerMiddleware()(https://redux-toolkit.js.org/api/createListenerMiddleware)中间件,监听X的更改,然后调用Y的reducers?
  • 或者,我应该在Y上添加一个createListenerMiddleware()中间件,监听X的更改,并根据自己的状态作出反应?

如果是第一种情况,有哪些好处/场景需要使用createListenerMiddleware(),而简单的thunk无法处理呢?

英文:

Let's say I have feature X and feature Y representing two different aspects of my react system. Each feature has its own redux state slice. In the following example, if X changes, Y should adjust its own state accordingly.

The project structure is as such:

src/features/feature-X/  {components/slice for featureX}
src/features/feature-Y/  {components/slice for featureY}
src/features/base/...etc.

Should I:

  • Use a thunk to call the respective reducers on both X and Y so that X and Y are not coupled? (if so, where would I put this thunk? Inside X, Y or somewhere else?)
  • Or, should I add a createListenerMiddleware() (https://redux-toolkit.js.org/api/createListenerMiddleware) middleware on X that listens for X's changes and then calls Y's reducers
  • Or, should I add a createListenerMiddleware() middleware on Y that listens for X's changes and reacts on its own state accordingly.

If the first, what benefit/scenarios require createListenerMiddleware() that a simple thunk wouldn't handle?

答案1

得分: 1

以下是翻译好的内容:

每个你定义的状态切片都可以声明 reducer 函数,这些函数可以响应外部动作。这些额外的 reducer 定义在切片的 extraReducers 属性上。

Redux 的一个关键概念是每个切片 reducer "拥有"自己的状态切片,许多切片 reducer 可以独立地响应相同的动作类型。extraReducers 允许 createSlice 响应除它生成的类型之外的其他动作类型。

示例:

切片 X

import { createSlice } from '@reduxjs/toolkit';
import { actionY } from './sliceY';

const sliceX = createSlice({
  name: "X",
  initialState,
  reducers: {
    actionX: (state, action) => {
      // 更新 X 状态
    },
  },
  extraReducers: builder => {
    builder
      .addCase(actionY, (state, action) => {
        // 基于动作 Y 更新 X 状态
      });
  },
});

export { actionX } = sliceX.actions;
export default sliceX;

切片 Y

import { createSlice } from '@reduxjs/toolkit';
import { actionX } from './sliceX';

const sliceY = createSlice({
  name: "Y",
  initialState,
  reducers: {
    actionY: (state, action) => {
      // 更新 Y 状态
    },
  },
  extraReducers: builder => {
    builder
      .addCase(actionX, (state, action) => {
        // 基于动作 X 更新 Y 状态
      });
  },
});

export { actionY } = sliceY.actions;
export default sliceY;
英文:

Each state slice you have defined can declare reducer functions that can respond to external actions. These extra reducers are defined on the slice's extraReducers property.

> One of the key concepts of Redux is that each slice reducer "owns" its
> slice of state, and that many slice reducers can independently respond
> to the same action type. extraReducers allows createSlice to respond
> to other action types besides the types it has generated.

Example:

Slice X

import { createSlice } from '@reduxjs/toolkit';
import { actionY } from './sliceY';

const sliceX = createSlice({
  name: "X",
  initialState,
  reducers: {
    actionX: (state, action) => {
      // update X state
    },
  },
  extraReducers: builder => {
    builder
      .addCase(actionY, (state, action) => {
        // update X state based on action Y
      });
  },
});

export { actionX } = sliceX.actions;
export default sliceX;

Slice Y

import { createSlice } from '@reduxjs/toolkit';
import { actionX } from './sliceX';

const sliceY = createSlice({
  name: "Y",
  initialState,
  reducers: {
    actionY: (state, action) => {
      // update Y state
    },
  },
  extraReducers: builder => {
    builder
      .addCase(actionX, (state, action) => {
        // update Y state based on action X
      });
  },
});

export { actionY } = sliceY.actions;
export default sliceY;

huangapple
  • 本文由 发表于 2023年6月1日 13:48:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定