Problem, as an interviewer would state it:
"Implement a minimal version of React's useState hook, WITHOUT React itself — explain, with a real working example, how state actually persists across re-renders when a component is 'just a function' that gets called again."
Examples:
const [count, setCount] = useState(0);
setCount(5);
// on the NEXT render, useState(0) returns 5, not 0 -- state persisted OUTSIDE the function call
Clarifying questions expected:
- Does this need to support MULTIPLE useState calls in one component, each with its own independent slot?
- Should setState support a functional-update form, like React's real setCount(prev => prev + 1)?
- Is this scoped to one component instance, or shared globally (a real, important distinction)?
Code / implementation expected: Yes — real, direct proof that state persists across simulated re-renders, that multiple useState calls stay independent, and that a functional update correctly reads the previous value.