Im trying to render a loading Spin while the fetch is running but the component is not rendering.
let spinning: boolean;
...
<Button htmlType="submit" onClick={() => spinning = true}>
{spinning ? <Spin /> : <></>}
</Button>
Im trying to render a loading Spin while the fetch is running but the component is not rendering.
let spinning: boolean;
...
<Button htmlType="submit" onClick={() => spinning = true}>
{spinning ? <Spin /> : <></>}
</Button>
2
Answers
You’ve overlooked the most fundamental part of React in every React tutorial… state. Just updating a variable doesn’t trigger a component to re-render. Updating state does:
If you want to trigger a loading boolean state when the component is fetching something from the API, the best approach would be to first create a state with
useState
hook.Then in your fetch logic, you should set the state to true, and after the fetch is done, set it back to false.