Question presented to candidate:
"Suppose you cannot use useState or useReducer. What other ways are there to hold state that belongs to a component?"
What a strong answer should cover:
- The historical answer: class components with
this.stateandthis.setState. Still fully supported in React 19 — verified working. this.setStatemerges partial state, unlike the hook setters which replace; and it takes an optional callback that runs after the commit.useReffor values that must persist across renders but must not trigger one — timer ids, previous values, DOM nodes, instance-like flags.- The key distinction: state drives rendering; a ref does not. If the UI must react to the value, it is state.
useSyncExternalStorefor reading state that lives outside React entirely.useStateanduseReducerare two faces of one primitive, so "without either" really means "outside the hooks state model".- Anti-patterns: a module-level variable (shared by every instance and invisible to React), or mutating a ref and expecting a re-render.
- Practical framing: this question is usually probing whether you understand when a value should cause a render, not whether you can recite class syntax.
Clarifying questions expected:
- "Does the UI need to update when this value changes?" — that single question decides state versus ref.
- "Is this a legacy codebase, or are we designing something new?"
Code / implementation expected: Yes — a class component with setState beside a useRef example showing why the ref does not re-render.