What is the useState hook?

Answer

useState is a React hook that adds state to a function component. const [state, setState] = useState(initialValue); returns a pair: the current state value and a function to update it. The initial value is used only on the first render. Updating state: setState(newValue) — React schedules a re-render with the new state. Functional updates: when the new state depends on the old state, use setState(prev => prev + 1) — this guarantees you get the latest value even if updates are batched. Object/array state: you must create new objects/arrays instead of mutating — setUser({ ...user, name: "Alice" }), not user.name = "Alice". Lazy initialization: pass a function to useState to compute the initial value only once (useful for expensive calculations): useState(() => computeExpensiveValue()). Multiple useState calls are independent. State is preserved across re-renders and reset only when the component unmounts or its key changes.