[英]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
. 我试图通过react-router将道具从一个组件传递到另一个组件。当我尝试从子组件获取该道具时,我收到此消息
TypeError: this.props.params.appState is undefined
。 Here is my code: 这是我的代码:
Tracks.jsx: Tracks.jsx:
import { observable } from 'mobx';
export default class Tracks {
@observable tracks = [];
}
app.jsx: 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: Album.jsx:
.....
@observer
export default class Album extends React.Component {
constructor(props) {
super(props);
}
componentDidMount () {
console.log(this.props.params.appState.tracks);
}
.....
change this 改变这一点
this.props.params.appState
to 至
this.props.route.appState
Params is dynamic segments of the URL, for props you need route Params是URL的动态段,用于需要路由的道具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.