I'm trying to dynamically load properties into a React component. The properties are set in this.props.customObjects using fetch with async/await higher up in the component structure.
My CustomObject component (below) receives the properties in the form of a promise, but I fail in actually showing the fetched data in the component. I guess my component renders before the promise has resolved.
What's the best way to solve this? Can values from async/await be used in props, or do I have to use state?
My component:
export default class CustomObject extends React.Component {
getCustomObject = (elementType) => {
const customObject = this.props.customObjects[elementType];
console.log('customObject', customObject);
return customObject;
}
componentDidMount() {
const customObject = this.getCustomObject(this.props.element.elementType)
this.setState({ customObject });
}
render () {
return <div className={this.props.className} style={this.props.style}>
{this.state.customObject.name} // Gives TypeError: Cannot read property 'customObject' of null
</div>
}
}
The console log looks like:
