I extract data from my "ComponentDidMount" function and load them in my state. Straight after I console log the value and see everything is there. However, when I try to access the state the same way in my return in my render function, I will get "undefined".
public componentDidMount() {
sp.web.lists.getByTitle("alert").items.get().then((item) => {
this.setState({items: item})
console.log(this.state.items[0].Title) //works
})
}
Above console log "(this.state.items[0].Title)" works. I try to use the state the same way in my render function:
<span className={ styles.title }>{this.state.items[0].Title}</span>
Does not work, and the value is undefined. How come? I store the data in my state when the component is loaded, and expected the data to be there when I refered to state in my render. How come?