有什么方法可以知道在 redux 中发送的最新动作
any way to know the latest action that was dispatched in redux
我正在开发一个 react-with-redux 应用程序我正在使用库 redux-undo 并且随着库功能的发展,它会侦听操作列表并在撤消时恢复到以前的状态发生。
场景:我有一个页面,其中的列表项将是 created/deleted 并且每当这些操作发生时都会进行 API 调用。用户可以撤消创建和删除操作。
我想知道有没有办法知道最近派发的action。
例如:如果用户创建了一个列表项并单击了撤消,我想知道派发的最新操作是创建,以便我可以恢复创建(通过创建 [删除列表项) =23=]调用)。
同样,如果用户删除了一个列表项,我想知道派发的最新操作是删除,以便我可以恢复删除(通过创建 [=23= 再次创建列表项) ] 调用,从过去的状态形状中获取详细信息并发送已删除列表项的详细信息)
请告诉我是否有任何方法可以实现此目的?
您需要找到一个备用存储空间 space 来存储不受 redux-undo
影响的最新操作,但它也是全局的,因此您可以在任何需要的地方访问它。
我推荐 local storage
解决方案。
在你的 reducer 中,你可以添加一个语句来设置哪个是最新发送到浏览器存储中的动作:
...
case CREATE: {
localStorage.setItem("latestAction", "CREATE");
return someNewState;
}
case DELETE: {
localStorage.setItem("latestAction", "DELETE");
return someNewState;
}
...
然后当您想从代码中的任何位置获取该值时:
localStorage.getItem("latestAction");
您可以使用 store.subscribe
,如前所述 here:
The easiest way is to have a reducer that remembers just the last action:
function lastAction(state = null, action) {
return action;
}
Then you can use store.getState().lastAction, assuming you did
something like
import { combineReducers, createStore } from 'redux';
const rootReducer = combineReducers({
someReducer,
someOtherReducer,
lastAction // <-- use it!
});
const store = createStore(rootReducer);
store.subscribe(() => {
console.log(store.getState().lastAction);
});
我正在开发一个 react-with-redux 应用程序我正在使用库 redux-undo 并且随着库功能的发展,它会侦听操作列表并在撤消时恢复到以前的状态发生。
场景:我有一个页面,其中的列表项将是 created/deleted 并且每当这些操作发生时都会进行 API 调用。用户可以撤消创建和删除操作。
我想知道有没有办法知道最近派发的action。
例如:如果用户创建了一个列表项并单击了撤消,我想知道派发的最新操作是创建,以便我可以恢复创建(通过创建 [删除列表项) =23=]调用)。
同样,如果用户删除了一个列表项,我想知道派发的最新操作是删除,以便我可以恢复删除(通过创建 [=23= 再次创建列表项) ] 调用,从过去的状态形状中获取详细信息并发送已删除列表项的详细信息)
请告诉我是否有任何方法可以实现此目的?
您需要找到一个备用存储空间 space 来存储不受 redux-undo
影响的最新操作,但它也是全局的,因此您可以在任何需要的地方访问它。
我推荐 local storage
解决方案。
在你的 reducer 中,你可以添加一个语句来设置哪个是最新发送到浏览器存储中的动作:
...
case CREATE: {
localStorage.setItem("latestAction", "CREATE");
return someNewState;
}
case DELETE: {
localStorage.setItem("latestAction", "DELETE");
return someNewState;
}
...
然后当您想从代码中的任何位置获取该值时:
localStorage.getItem("latestAction");
您可以使用 store.subscribe
,如前所述 here:
The easiest way is to have a reducer that remembers just the last action:
function lastAction(state = null, action) {
return action;
}
Then you can use store.getState().lastAction, assuming you did something like
import { combineReducers, createStore } from 'redux';
const rootReducer = combineReducers({
someReducer,
someOtherReducer,
lastAction // <-- use it!
});
const store = createStore(rootReducer);
store.subscribe(() => {
console.log(store.getState().lastAction);
});