You need to give react time to update the screen before starting the sleep/heavy work process.
Set a after a timer:
setTimeout(() => {
setA((a) => a + 1);
}, 50)
This will allow react to udapte screen with loading === true and after that, it will work on the process.
You could also try to use a webworker to offload the work form the main thread to not block the rendering. Check out useWorker.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…