React Redux :如何找到在 Redux 中调用的最后一个操作并再次触发相同的操作



我有多个地方在单击时执行不同的操作。

我想找到已执行的最新操作,然后仅使用"续订"按钮再次执行它。

如何查找调用的最后一个操作? 它必须在 redux 系统中的某个地方。 我只需要知道如何找到它。

如何查找调用的最后一个操作?它必须在 redux 系统中的某个地方。

我不知道 redux 保存过去操作的任何地方。但是,您可以创建自定义中间件来拦截和保存操作。像这样:

let lastAction = null;
export const getLastAction = () => lastAction;
export const lastActionMiddleware = store => next => action => {
lastAction = action;
return next(action);
}
// In another file, initialize your store with the middleware
// Your code may look different here if you already have middlewares:
import { createStore, applyMiddleware } from 'redux';
import { lastActionMiddleware } from 'whatever/the/path/is';
const store = createStore(
rootReducer, 
rootState, 
applyMiddleware(lastActionMIddleware)
)
// And in yet another file, you can look up the most recent action
import { getLastAction } from 'whatever/the/path/is';
// ...
componentDidMount() {
const lastAction = getLastAction();
console.log('the last thing dispatched anywhere was:', lastAction)
}

最新更新