RepoPilot

Where in the source is useState defined?

About facebook/react · 2 messages · last activity 7/5/2026

Question

Where in the source is useState defined?

RepoPilot

useState is defined in [packages/react/src/ReactHooks.js:L66-L72].

Here's the implementation:

export function useState<S>(
  initialState: (() => S) | S,
): [S, Dispatch<BasicStateAction<S>>] {
  const dispatcher = resolveDispatcher();
  return dispatcher.useState(initialState);
}

The function accepts an initial state (either a value or a function that returns a value) and delegates to the dispatcher's useState method. The dispatcher is resolved at runtime via resolveDispatcher(), which allows React to use different implementations depending on the environment (like during rendering vs. outside of a component).

FOLLOW_UPS:

  • What does resolveDispatcher function do?
  • How does useState handle initial state functions?
  • What is the Dispatch type signature?

Want to ask your own question?

Open chat for facebook/react

Public Q&A. Generated by RepoPilot from the actual source of facebook/react. AI answers can be incomplete or stale — verify before relying on them.