Advanced React Interview QuestionsAdvancedConcept
React · Question 100
What does flushSync do, and why is it considered a last-resort React API?
Direct answer
flushSync forces React to synchronously flush qualifying updates and update the DOM before returning, which is useful for rare browser or third-party integration requirements but can hurt performance and interfere with normal scheduling.
print.js
function handleBeforePrint() {
flushSync(() => {
setIsPrinting(true);
});
// By here, the DOM reflects isPrinting.
window.print();
}- Useful when: an external browser or third-party API requires the DOM to reflect a React update synchronously before the next statement.
- Performance cost: it bypasses normal scheduling advantages and may force other pending work to flush.
- Suspense impact: a synchronous flush can cause pending Suspense fallbacks to appear.