I am using react-router(v3) for routing purpose in my app. I have some nested route and want to access parent component state in child component.
Route:
<Route component={Main}>
<Route path="home" component={Home} />
</Route>
Main Component(parent):
export default class Main extends Component {
constructor(prop){
super(prop);
this.state = {
user : {}
}
}
render(){
return (
<div>
Main component
</div>
)
}
}
Home Component(child):
export default class Home extends Component {
constructor(prop){
super(prop);
}
render(){
return (
<div>
Main component
want to access user data here. how to access user from parent component?
</div>
)
}
}
In Home
component i want to access user data, that is in parent component Main
. Is there any way to access parent component state in child component ?
state
asprops
to Home component or In your parent, you can create a function likeaddUser
which will do the requiredsetState
and then pass that function asprops
to the child component. – Nicholas