As was pointed out to me in another question, if I have a function call without an argument in setState
, randomQuoteIndex()
, and the function uses a state set in that setState
, it's called before setState
.
componentDidMount() {
fetch('https://gist.githubusercontent.com/nataliecardot/0ca0878d2f0c4210e2ed87a5f6947ec7/raw/1802a693d02ea086817e46a42413c0df4c077e3b/quotes.json')
.then(response => response.json())
.then(quotes => this.setState({
quotes,
randomQuoteIndex: this.randomQuoteIndex(),
isDoneFetching: true
}));
}
randomQuoteIndex() {
return random(0, this.state.quotes.length - 1);
}
This results in an error because the state of quotes
isn't available at the time of randomQuoteIndex()
call.
However, if I change randomQuoteIndex()
to instead use a passed in quotes
parameter, as below, it works.
componentDidMount() {
fetch('https://gist.githubusercontent.com/nataliecardot/0ca0878d2f0c4210e2ed87a5f6947ec7/raw/1802a693d02ea086817e46a42413c0df4c077e3b/quotes.json')
.then(response => response.json())
.then(quotes => this.setState({
quotes,
randomQuoteIndex: this.randomQuoteIndex(quotes),
isDoneFetching: true
}));
}
randomQuoteIndex(quotes) {
return random(0, quotes.length - 1);
}
This is not what I expected; I had assumed that the state of quotes
would be available at the time randomQuoteIndex()
is called since it was called within setState
. Why is randomQuoteIndex()
called before setState
even though it's inside it?