简体   繁体   English

如何在使用React-Router&Redux时单击链接时调度操作?

[英]How to dispatch an action when clicking on Link when using React-Router & Redux?

Let's say I have a Link that sends me to a page for adding/editing a list entry. 假设我有一个Link ,它将我发送到一个页面,用于添加/编辑列表条目。

How do I dispatch a Redux action when I click on the Link itself so that I can update the Redux store first, before actually getting redirected to that page. 当我单击Link本身时,如何调度Redux操作,以便在实际重定向到该页面之前,我可以先更新Redux存储。

Eg: I click on Edit button -> Action is dispatched -> Store updated, {'state': 'edit-mode'} -> Proceed to redirect. 例如:我点击编辑按钮 - >调度操作 - >存储更新, {'state': 'edit-mode'} - >继续重定向。

Or do you have another way in mind to accomplish what I'm trying to do? 或者你有另一种方法来完成我想要做的事情吗?

Maybe when component has mounted, then I will run an action like stateToEdit based on certain conditions? 也许当组件已经安装时,我会根据某些条件运行stateToEdit类的操作? If so, then please show to me your way. 如果是的话,那么请向我展示你的方式。 Thanks 谢谢

PS: I'm using only one component for all add/edit/delete. PS:我只使用一个组件进行所有添加/编辑/删除。 So I'm thinking of a way to render based on the state whether its on edit-mode or delete-mode etc. 所以我正在考虑一种基于state进行渲染的方法,无论是edit-mode还是delete-mode等。

Here are a couple ways you could go about addressing this issue: 您可以通过以下几种方式解决此问题:

  1. Instead of using Link , try utilizing browserHistory.push(path) with an onClick function. 而不是使用Link ,尝试使用带有onClick函数的browserHistory.push(path)
    • Inside the onClick , you can dispatch your action, then push to a new location. onClick ,您可以dispatch您的操作,然后push送到新位置。
    • However, if you want to perform this series of actions in various components, you will probably suffer from code duplication. 但是,如果要在各种组件中执行这一系列操作,则可能会遇到代码重复问题。
  2. A more robust way to address this issue would be to implement redux-thunk , which provides a generic way of performing multiple "actions" (be it calling a Redux action, or performing an async operation, or both!) in response to a change. 解决此问题的一种更强大的方法是实现redux-thunk ,它提供执行多个“操作”的通用方法(无论是调用Redux操作,还是执行异步操作,或两者兼而有之!)以响应更改。

Redux thunk action Redux thunk行动

export const dispatchThenRoute = (myAction, myPath) => {
    return (dispatch) => {
        dispatch(myAction)
        browserHistory.push(myPath);
    }
}; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用react-router v4调度redux动作? - How to dispatch redux actions using react-router v4? react-router v4在页面更改上调度redux操作 - react-router v4 dispatch redux action on page change 隐藏菜单并在单击react-router链接时重定向 - Hide menu and redirect when clicking on react-router Link 在点击时,react-router 4不会更新UI <Link> - react-router 4 doesn't update UI when clicking <Link> 单击react-router Link时,如何将状态传输到父组件? - How do I transmit a state to a parent component when clicking a react-router Link? 使用 react-router 5 和 redux 7 react-router<Link> 进入新路线后不重置状态 - While using react-router 5 with redux 7 react-router <Link> is not resetting state after going to new route 使用react-router / react-router-redux将特定的redux操作绑定到一个路由 - Bind specific redux action to one Route using react-router / react-router-redux 结合 React、Redux 和 React-Router 的正确方法是什么? - What is the correct approach when combining React, Redux and React-Router? 使用redux-react with react-router时,prop'component'无效 - Invalid prop 'component' when using redux-react with react-router React-Router:为什么不呢 <Link> 组件的onClick处理程序调度我的动作? - React-Router: Why won't <Link> component's onClick handler dispatch my action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM