1

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.

Here's my code:

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)
index
  • 3,697
  • 7
  • 36
  • 55

1 Answers1

4

Well you have 3 "export default" when you are supposed to have only one per file.

Your component should look like this

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)
QoP
  • 27,388
  • 16
  • 74
  • 74
  • Oh really!? Ehehe. But ever since I have had two which is `export default class ThisComponent extends React.Component` and `export default connect...`. – index May 04 '16 at 10:18
  • you can export them individually but you should have only one default. Didn't it work? – QoP May 04 '16 at 10:27
  • Yes it did work. So I've been implementing all my components incorrectly as well. Haha! When I have `connect` without `withRouter`, should the `export default` be on `connect` or still on `.. React.Component`? – index May 05 '16 at 05:26
  • it should be on connect :-P – QoP May 05 '16 at 06:05
  • So really just one export default. Got it! Thanks so much for that additional lesson. – index May 05 '16 at 08:57