3

I am trying to pass props from one component to another via react-router.When I try to get that props from child component, I got this message TypeError: this.props.params.appState is undefined . Here is my code:

Tracks.jsx:

import { observable } from 'mobx';


export default class Tracks {
    @observable tracks = [];

}

app.jsx:

.......
import Tracks from './store/Tracks.jsx';

......
const appState = new Tracks();

ReactDOM.render(
    <Router history={browserHistory} >
        <Route path="/" component={Login} />
        <Route path="/dashboard" onEnter={authRequired} component={Main}>
            <Route path="album/create" component={AlbumCreate} />
            <Route path="album/:id" component={Album} appState={appState}/>
            <Route path="album/:id/upload"  component={Upload} />
        </Route>
    </Router>,
    document.getElementById('app')
);

Album.jsx:

.....
@observer
export default class Album extends React.Component {

    constructor(props) {
        super(props);
    }
componentDidMount () {
        console.log(this.props.params.appState.tracks);
    }

.....
pyprism
  • 2,928
  • 10
  • 46
  • 85
  • Could you check what contains `this.props` ? – Mario Santini Jul 06 '16 at 14:28
  • As I can see error says ``TypeError: this.props.params.appState is undefined`` so it seems that ``appState`` is ``undefined`` not ``this.props`` itself. – Kamil Jul 06 '16 at 14:28
  • @MarioAlexandroSantini `Object { history: Object, location: Object, params: Object, route: Object, routeParams: Object, routes: Array[2], children: null, 2 more… }` – pyprism Jul 06 '16 at 14:32
  • @Kamil I just want to see if the poperty was direct in `this.props` or not. – Mario Santini Jul 06 '16 at 14:38
  • 1
    [Here](http://stackoverflow.com/questions/27864720/react-router-pass-props-to-handler-component?rq=1) there is an example on how to do it. Please check and come back if you have troubles. – Mario Santini Jul 06 '16 at 14:42

1 Answers1

6

change this

this.props.params.appState

to

this.props.route.appState

Params is dynamic segments of the URL, for props you need route

Kokovin Vladislav
  • 10,241
  • 5
  • 38
  • 36