反应路由器升级与路由器冲突

react-router upgrading conflict withRouter

我看过升级指南 here 如何使用 withRouter 但我在我的项目中实施它时遇到问题。我的 connect 有问题。当我的 connect 高于 withRouter 时,router 存在于 props 而不是我的状态,当我的 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)

嗯,你有 3 个 "export default",而你应该每个文件只有一个。

您的组件应如下所示

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)