Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React props is undefined

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);
    }

.....
like image 551
pyprism Avatar asked Sep 01 '26 01:09

pyprism


1 Answers

change this

this.props.params.appState

to

this.props.route.appState

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

like image 185
Kokovin Vladislav Avatar answered Sep 03 '26 14:09

Kokovin Vladislav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!