react-router升级与路由器冲突

时间:2016-05-04 04:56:21

标签: javascript reactjs react-router router

我看过升级指南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)

1 个答案:

答案 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)