DEV Community

Discussion on: React, React-Router and Fetch API

Collapse
 
dance2die profile image
Sung M. Kim • Edited

Hi @iamkael .

Call fetch as a callback of this.setState().

this.setState({
    loading: !loading,
    url
}, () => fetch(`/w/${url}`).<rest of the chain>...)

My guess is that, handleClick's first this.setState sets the loading property, which is not properly recognized as this.setState is an asynchronous operation.

You'd fetch after loading property is turned off if you want such a synchronous behavior.