I am making a fetch request that returns an array of strings.
fetch(linkFetch)
.then(resp => resp.json())
.then(arr => {
that.setState({
images: arr
});
})
.then(this.test());
on the last line you can see I try to call the method test()
which is just trying to access the state that was set in the fetch()
request.
test(){
console.log(this.state.images[1]);
}
console logs 'undefined'
However, if I assign test()
to a button or something so that I can manually call it works fine which leads me to believe that when I call .then(this.test());
in the fetch request it is actually being called before the state is set.
How can I make sure it gets called after the state has been set in the fetch request?