Irbisa · Cheatsheet
Rendering & Reconciliation
What triggers a render, render vs commit, reconciliation and keys, Fiber, concurrent rendering, transitions, Suspense, hydration
What causes a React component to re-render?
What is the difference between a React element, a component and a component instance?
function Avatar({ name }: { name: string }) { const [expanded, setExpanded] = useState(false); return <button onClick={() => setExpanded(true)}>{expanded ? name : name[0]}</button>;} const element = <Avatar name="Ada" />;…What is the difference between the render phase and the commit phase in React?
In what order do these logs appear on mount (without
StrictMode), and why?function Child() { console.log('Child render'); useLayoutEffect(() => console.log('Child layout effect'), []); useEffect(() => console.log('Child effect'), []); return <p>child</p>;} function Parent() { console.log('Parent render'); useLayoutEffect(() => console.log('Parent layout effect'), []); useEffect(() => console.log('Parent effect'), []); return <Child />;}What is reconciliation, and which heuristics does React use to diff two element trees?
function Page({ isAdmin }: { isAdmin: boolean }) { // same type at the same position: Panel keeps its state when isAdmin flips return isAdmin ? <Panel title="Admin" /> : <Panel title="User" />;} function Card({ boxed }: { boxed: boolean }) {…How many times does
Expensivelog after the button is clicked three times, and why?function Expensive() { console.log('Expensive render'); return <p>Heavy chart</p>;} function Counter({ children }: { children: React.ReactNode }) { const [count, setCount] = useState(0); return ( <div> <button onClick={() => setCount(count + 1)}>Clicked {count}</button> {children} </div> );} export default function App() { return <Counter><Expensive /></Counter>;}What happens if you call a state setter during render, and when is that a legitimate pattern?
function List({ items }: { items: Item[] }) { const [selection, setSelection] = useState<Item | null>(null); const [prevItems, setPrevItems] = useState(items); if (items !== prevItems) { // runs once per change; React re-runs List before rendering its children…What is concurrent rendering, and what changed with React 18?
How does an update inside
startTransitionrender differently from an urgent update, and what happens if the user types while it is rendering?function SearchPage() { const [text, setText] = useState(''); const [query, setQuery] = useState(''); const [isPending, startTransition] = useTransition(); return (…What does
useDeferredValuedo, and when would you use it instead of debouncing?const SlowList = memo(function SlowList({ query }: { query: string }) { const items = filterHugeList(query); // expensive return <ul>{items.map((item) => <li key={item.id}>{item.name}</li>)}</ul>;}); function Search() {…What happens inside React when a component suspends, and what did React 19 change about it?
const Comments = lazy(() => import('./Comments')); function Post({ postPromise }: { postPromise: Promise<PostData> }) { const post = use(postPromise); // suspends until resolved return ( <article>…What is hydration, and what does selective hydration add in React 18 and later?
// entry: attach to the server HTML instead of creating new DOMhydrateRoot(document.getElementById('root')!, <App />); function LocalTime() { // wrong: the server and the client format different times // return <span>{new Date().toLocaleTimeString()}</span>;…What is React Fiber, and what problem did it solve?
How does React reconcile a reordered keyed list, and which reorder is the most expensive for it?
How does React schedule and prioritise updates internally: what are lanes and time slicing?
What are signals in Solid, Preact or Angular, and why does React re-render components instead of using fine-grained updates?
type Effect = () => void;let running: Effect | null = null; function createSignal<T>(value: T) { const subscribers = new Set<Effect>(); const read = () => {…Why does a navigation wrapped in a transition keep the old page on screen instead of showing a
Suspensefallback, and how do you control which fallbacks appear?function App() { const [page, setPage] = useState('/'); const [isPending, startTransition] = useTransition(); function navigate(url: string) { // keeps the current page on screen while the next one loads…Find the bug: the tab switch is wrapped in
startTransition, yet after theawaitthe old tab disappears and a Suspense fallback flashes.function Tabs() { const [tab, setTab] = useState<'about' | 'posts'>('about'); const [isPending, startTransition] = useTransition(); function selectTab(next: 'about' | 'posts') { startTransition(async () => { await saveLastTab(next); setTab(next); }); } return ( <> <TabBar active={tab} pending={isPending} onSelect={selectTab} /> <Suspense fallback={<Spinner />}>{tab === 'about' ? <About /> : <Posts />}</Suspense> </> );}What does
flushSyncdo, and when is it actually needed?import { flushSync } from 'react-dom'; function Chat() { const [messages, setMessages] = useState<Message[]>([]); const listRef = useRef<HTMLUListElement>(null);…What happens when a component throws during render, and what are
onCaughtError,onUncaughtErrorandonRecoverableErrorin React 19?import { createRoot } from 'react-dom/client'; const root = createRoot(document.getElementById('root')!, { onCaughtError(error, errorInfo) { report(error, { componentStack: errorInfo.componentStack, handled: true }); },…