我试图在通过 React Route(重定向到)重定向到路由时发送props,但组件(SideBar)没有收到任何props。我应该如何发送props以及如何在组件中接收props?我想从当前状态发送一个props。
父组件 (WelcomeScreen.tsx)
render(){
return (
this.state.idUser != -1 ? <Redirect to={{
pathname: '/Activities',
state: { referrer: this.state.idUser }
}}/> :
<div>
{this.renderHeader()}
<div className='screenUser'>
{this.renderPanelUpButtons()}
{this.renderWelcomeMenu()}
</div>
</div>
);
活动.tsx
export interface ActivitiesProps {
idUser: number;
}
interface ActivitiesState {
message: string;
activitiesFirstPart: IActivity[];
activitiesSecondPart: IActivity[];
}
componentWillReceiveProps(newprops : ActivitiesProps){
if(newprops!== this.props){
this.props = newprops;
}
}
这里组件不会收到任何props。
为了更容易理解,我还列出了路线;
render() {
return (
<div className="hangouts-body">
<div>
<Route path="/" exact={true} component={WelcomeScreen} />
<Route path="/Groups" exact={true} component={Groups} />
<Route path="/Activities" exact={true} component={Activities} />
<Route path="/OneActivity" exact={true} component={OneActivity} />
</div>
</div>
);
}