javascript 连接和路由器问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54247082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:22:31  来源:igfitidea点击:

connect and withRouter issue

javascriptreactjsreduxreact-reduxfrontend

提问by xxddoo

I am using Redux and React for my project. I have some Routes in App.js. I also use the connect function in react-redux in my project. To prevent update blocking issue, I usually wrapped my component in this way

我在我的项目中使用 Redux 和 React。我在 App.js 中有一些路由。我也在我的项目中使用 react-redux 中的 connect 函数。为了防止更新阻塞问题,我通常以这种方式包装我的组件

withRouter(connect(mapStateToProps, mapDispatchToProps)(App)),

However, If I changed order of withRouter and connect it doesn't work:

但是,如果我更改了 withRouter 的顺序并连接,则它不起作用:

connect(mapStateToProps, mapDispatchToProps)(withRouter(App))

I have console.log the props in App.js. It already receives location and history props. I am figuring out the theory behind why the order does matter ?

我在 App.js 中有 console.log 道具。它已经收到位置和历史道具。我正在弄清楚为什么顺序很重要背后的理论?

采纳答案by Aashish Karki

Could you refer to this https://reacttraining.com/react-router/core/api/withRouter, it clearly says that it doesn't work the other way around

你能参考这个https://reacttraining.com/react-router/core/api/withRouter,它清楚地表明它不能反过来工作

回答by Wesley Amaro

You can use it with the method composefrom redux library.

您可以将它与composeredux 库中的方法一起使用。

export default compose(
  withRouter,
  connect(mapStateToProps, mapDispatchToProps)
)(App);

回答by Nabnit Jha

If someone has still issue then plz follow this one

如果有人仍然有问题,那么请按照这个

const ShowTheLocationWithRouter = withRouter(Login);

const ShowTheLocationWithRouter = withRouter(Login);

export default connect(mapStateToProps, mapDispatchToProps)(ShowTheLocationWithRouter);

导出默认连接(mapStateToProps,mapDispatchToProps)(ShowTheLocationWithRouter);