- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想创建一个可重用的 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/
当包裹在 EmberJS Controller 的 actions 中时,如何从另一个 Action 调用一个 Action ? 使用现已弃用的方式定义操作的原始代码: //app.js App.In
我有一个 Action (一个yaml文件),用于将docker镜像部署到Google Cloud Run。 我希望收到通知构建和推送结果的Slack或电子邮件。 构建操作完成后,如何触发消息操作?
Selenium 的 actions 类中存在的 tick(Action action) 和 tick(Interaction...actions) 方法的用途是什么? 是否与点击任何 webElem
简短的背景故事 我们目前为数百名用户提供对话操作。我们在过去三年中为我们的一位客户开发了这个 Action 作为“工作”。正如我们最近发现的那样,我们会受到对话行为的影响。 当然,我们现在正在研究如何
考虑系统用户可以并发方式执行两个操作,第一个操作 (A1) 仅对用户的订单执行,第二个操作 (A2) 包括在执行时执行 (A1),如下面的使用所述-案例图..((考虑A1完全执行U1,A2完全执行U2
我正在为 android 中的 ActionBar 而苦苦挣扎。 这是我的问题:我的操作项没有显示在操作栏中,而是堆叠在操作溢出中,无论我做什么.. 我花了一天的时间寻找解决方案,但我似乎找不到缺少的
我正在构建一个工作流,其中一个操作为工作流中的一个步骤提供条件。我该如何使用这个值? 该操作的值为空,因此计算结果为 false,并且从未部署过任何内容... jobs: build: s
鉴于您有一些全局 View (例如,显示加载屏幕),您可能希望在许多情况下发生这种情况,为该行为创建一个 Action 创建者/ Action 对还是为相关 Action 创建 reducer 更合适
我有一个使用 DialogFlow 构建的 Actions on Google 代理,其中包含多个操作(例如 actions.intent.MAIN 和 get_day_of_week)。 当我在 3
是否可以从我的 action.yml 文件中引用另一个 GitHub 操作? 请注意,我在这里谈论的是操作,而不是工作流程。我知道这可以通过工作流来完成,但是操作可以引用其他操作吗? 最佳答案 答案似
在 Vuex 操作中,我们有以下实现。 async actionA({ commit, dispatch }) { const data = this.$axios.$get(`/apiUrl`)
我正在将我的应用程序服务器从 Jboss 4.2 迁移到 7.1。我在 Struts 配置中收到以下错误。 struts.xml 中定义的 Action 被调用,而 Action 包中的操作未被调用。
我向 ActLand 发送请求,然后 intercept(),如果没有登录则重定向到 Login.jsp。 struts.xml:
我有一个 Action 创建器,它接受一个 id 和一个回调函数。它向服务器发送请求以执行某些操作并返回一个虚拟操作。我在这里想做的就是调用回调函数并退出,因为该虚拟操作对我来说没有用处,例如喜欢帖子
我已经使用 Html.Action 方法调用了另一个 View 。当用户单击操作链接时,我想在 subview 内使用参数调用相同的操作。 当我写这段代码时,我得到了这个错误信息: Html.Acti
是 public event Action delt = () => { Console.WriteLine("Information"); }; 的重载版本 Action delg = (a, b)
countresultsfrom.addActionListener(new ActionListener() { public void actionPerforme
我刚刚看到一个 brand-new video在 Rx 框架上,一个特别的签名引起了我的注意: Scheduler.schedule(this IScheduler, Action) 在 23:55,
我创建了一个在我的开发者帐户中完美运行的 DialogFlow 应用程序。 但我需要以另一个用户的身份对其进行测试,因此在我的 Google Action 模拟器中,我添加了另一个测试帐户作为项目的所
我正在尝试实现消息存储拦截器以在我的 JSp 上显示 ActionMessage,但无法访问 ActionMessage。有人可以提供一个链接如何实现消息存储拦截器吗? 最佳答案 这是我的一个应用程序
我是一名优秀的程序员,十分优秀!