当用户单击浏览器后退/前进按钮时,我正在使用 redux-undo 来操作活动/非活动按钮状态。我的函数 changeButtonImage() 根据当前的 redux 状态更改活动/非活动菜单按钮图像。我被卡住了,因为 mapStateToProps 在重新渲染后更改为新值。但我需要从“浏览器后退按钮处理程序”调用 changeButtonImage()。我找到了一种解决方法来调用 store geState() 但它是反模式方式。请解释我如何以正确的方式欺骗它。
-> console.log('goBack');
console.log(
'before',
isClickItem // mapStateToProps property
// store.getState().itemReducer.present.isClickItem //-> anti-pattern
);
onUndo();
console.log(
'after',
isClickItem // --> nothing changes before re-render
// store.getState().itemReducer.present.isClickItem //-> change but anti-pattern
);
changeButtonImage(); // --> use before undo state