本文介绍了react-router升级与Router冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里看到了升级指南 如何使用 withRouter
但我在我的项目中实现它时遇到问题.我的 connect
有问题.当我在 withRouter
上面有我的 connect
时,router
存在于 props 而不是我的状态,当我有 connect
在 withRouter
下方,状态存在但不存在路由器.
I've seen an upgrade guide here how to use withRouter
but I have a problem implementing it on my project. It's having problem with my connect
. When I have my connect
above withRouter
, router
exists on props but not my states, and when I have connect
below withRouter
, states exist but not router.
这是我的代码:
import React from 'react';
import { withRouter } from 'react-router';
import moment from 'moment';
import { connect } from 'react-redux';
export default class ThisComponent extends React.Component {
render() {
console.log(this.props)
return ...
}
});
export default connect(state => ({ oneState: state.oneState, twoState: state.twoState }))(ThisComponent)
export default withRouter(ThisComponent)
推荐答案
当您应该每个文件只有一个时,您有 3 个默认导出".
Well you have 3 "export default" when you are supposed to have only one per file.
你的组件应该是这样的
import React from 'react';
import { withRouter } from 'react-router';
import moment from 'moment';
import { connect } from 'react-redux';
class ThisComponent extends React.Component {
render() {
console.log(this.props)
return ...
}
});
ThisComponent = connect(state => ({ oneState: state.oneState, twoState: state.twoState }))(ThisComponent)
export default withRouter(ThisComponent)
这篇关于react-router升级与Router冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!