gpt4 book ai didi

javascript - 监听 redux Action

转载 作者:搜寻专家 更新时间:2023-11-01 04:35:51 24 4
gpt4 key购买 nike

我想创建一个可重用的 redux 表模块,它将存储和更新页码、显示的总页数等,我可以在所有页面之间共享这些信息。

但是我需要更新操作来触发刷新数据操作,该操作将根据页面命中不同的端点。

因此,也许某些特定于页面的内容会监听“RefreshData”操作,然后触发另一个操作。实现这一目标的最佳方法是什么?我目前正在为我的中间件使用 redux-thunk,但一直在寻找 redux-saga。

实现此目标的最佳方法是什么?

** 澄清**

我在多个页面/项目上都有表格,我想尽量减少代码重复。我在想我可以做一个可删除的 redux 模块(action + reducer),然后单独指定 REFRESH_DATA 操作的处理程序,这样我就不需要在这个文件中放置开关。我认为使用 redux-saga 我可以监听 REFRESH_DATA 操作并根据当前页面进行切换?或者有什么更好的建议。

export const ITEMS_PER_PAGE_UPDATED = 'ITEMS_PER_PAGE_UPDATED'
export const CURRENT_PAGE_UPDATED = 'CURRENT_PAGE_UPDATED'
export const REFRESH_DATA = 'REFRESHDATA'
export const DATA_REFRESHED = 'REFRESHDATA'

export function currentPageUpdated(value) {
return function (dispatch) {
dispatch({
type: CURRENT_PAGE_UPDATED,
value
})
dispatch({type:REFRESH_DATA})
}
}

export function itemsPerPageUpdated(value) {
return function (dispatch) {
dispatch({
type: ITEMS_PER_PAGE_UPDATED,
value
})
dispatch({type:REFRESH_DATA})
}
}

function reducer(state={ pageNumber:1, pageSize:10, totalItems:0, totalPages:1, sortColumn:null, sortAscending:true }, action) {
switch(action.type) {
case ITEMS_PER_PAGE_UPDATED:
return Object.assign({}, state, {pageSize: action.value, pageNumber:1})
case CURRENT_PAGE_UPDATED:
return Object.assign({}, state, {pageNumber: action.value})
case DATA_REFRESHED:
return Object.assign({}, state, {totalPages: action.values.totalPages, totalItems:action.values.totalItems})
default:
return state
}
}

export default reducer

最佳答案

如果我没理解错的话,听起来 REFRESH_DATA 操作主要是一种副作用,这意味着该操作仅作为另一个操作的结果被分派(dispatch)。如果是这样的话,那么我会说 redux saga 会是一个不错的选择,因为它可以在后台“监听” Action ,然后触发其他 Action 。但是,您可以通过自定义 middleware 走得更远。 .

使用自定义中间件,您可以在操作发送到 reducer 之前拦截它们。如果您要执行一系列与更新表和刷新数据相关的操作,那么为所有这些类型的操作创建一个通用的操作形状可能很有意义,这样您的中间件就可以监听它并进行调度特殊的副作用 Action 。

一个简单的实现可能看起来像这样:

中间件/table.js

import fetchData from '../somewhere';
import { DATA_REFRESHED } from '../actions';

// Make a unique identifier that you're actions can send so you're
// middleware can intercept it
export const UPDATE_TABLE = Symbol('UPDATE_TABLE');

export default store => next => action => {
const tableUpdate = action[UPDATE_TABLE];

// If the action doesn't have the symbol identifier, just move on
if (typeof tableUpdate === 'undefined') {
return next(action);
}

// At this point, you know that the action that's been dispatched
// is one that involves updating the table, so you can do a fetch
// for data

// Get whatever data you need to fetch
const { id, type, data } = tableUpdate;

// Dispatch the actual type of action to the store
next({ type, data });

// Go and fetch the data as a side-effect
return fetchData(id)
.then(response => next({ type: DATA_REFRESHED, data: response }));
}

actions.js

import { UPDATE_TABLE } from '../middleware/table.js';

// An action creator to dispatch the special table update action
// This is a simple example, but you'd probably want to be consistent
// about how the table update actions are shaped. The middleware
// should be able to pick out all of the data it needs to fetch data
// from whatever is sent with the action
function updateTable(action) {
return {
[UPDATE_TABLE]: action
};
}

export function currentPageUpdated(value) {
return updateTable({
type: CURRENT_PAGE_UPDATE,
value
});
}

我从现实世界的例子中得到了很多:https://github.com/rackt/redux/blob/master/examples/real-world/middleware/api.js .

关于javascript - 监听 redux Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34949597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com