我看过升级指南here如何使用withRouter
但我在项目中实现它时遇到问题。我的connect
有问题。如果我connect
withRouter
以上,router
存在于道具而不是我的州,当我connect
低于withRouter
时,状态存在但路由器不存在
这是我的代码:
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)
答案 0 :(得分:4)
如果每个文件只有一个,那么你有3个“导出默认值”。
您的组件应如下所示
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)