I am fetching data inside componentDidMount but i get undefined during initial render and again render happens and during that time the state variable gets populated. Now when it is not undefined and after population I want to destructure it and display the data inside my component.
Note: getProjectDetails() makes a GET req to populate the data.
I am getting typer error name of undefined.
constructor(props) {
super(props);
this.state = {
projectDetails: ''
};
}
componentDidMount() {
getProjectDetails(this.props.logged_in_user, this.props.projectId)
.then( res => {
this.setState({projectDetails: res});
})
.catch(err => {
console.log('Error: ' + err);
})
}
//Inside render()
render() {
console.log('Project details from API endpoint: ', this.state.projectDetails.project)
const { projectDetails } = this.state;
console.log('Destructure: ', projectDetails);
const project = this.state.projectDetails.project;
let {
name,
location,
city,
builder_name } = project;