Reputation: 41
COnsider the following:
componentDidMount(){
this.props.requestUser(this.props.match.params.userID).then(res =>
{
this.pageOwner = res.user;
debugger;
})
}
Why is my this.pageOwner still returning undefined? I have been trying my as off to get a user into a state that my user profile can actually acces.
Upvotes: 0
Views: 55
Reputation: 859
As @colin said, put it in this.state
. If you are getting an error in the render()
function, then just make your render like so:
constructor() {
super();
this.state = {
pageOwner: null
}
}
componentDidMount() {
this.props.requestUser(this.props.match.params.userID).then(res =>
{
this.setState({pageOwner: res.user});
debugger;
})
}
render() {
if(this.state.pageOwner)
return ( ... )
else
return null;
}
Upvotes: 0
Reputation: 17239
If you're trying to access this.pageOwner
, you need to do so after the this.props.requestUser()
async function has completed. You're currently getting the user in componentDidMount()
, so your render()
will initially see this.pageOwner
as undefined
until the async call is finished.
Additionally, it might be better to store the result of your async call in state
so that your component will re-render once the value is filled.
Upvotes: 1