liveBook · Manning

React hook

As explained in React Hooks in Action: With Suspense and Concurrent Mode

React Hooks are functions that let React function components access state, lifecycle behavior, and other React capabilities.

A React hook is a function that lets a React function component connect to state, context, lifecycle behavior, or side effects. Hooks let function components manage capabilities once associated mainly with class components, while keeping related UI logic together. section 1.2 section 1.3

Common hooks support distinct parts of component behavior:

  • useState and useReducer manage state and provide updater functions that can cause React to rerun the component and update its UI. section 2.4
  • useEffect performs and cleans up side effects such as fetching data, subscribing to a service, or working with the document. section 1.3.1 section 9.1
  • useContext accesses application state through context, while custom hooks package related state and behavior for reuse across components. section 1.2 section 1.3.2
  • Call hooks at the top level of function components or custom hooks; do not place them in conditionals, loops, nested functions, or ordinary JavaScript functions. section 9.2.1 section 9.2.2

Quiz component with local state and effects

A Quiz function component can use state for its current question and use effects to load question data and subscribe to online users. Related fetching and subscription logic can then move into reusable useFetch and useUsers custom hooks, which Quiz and Chat can both call. section 1.3.1 section 1.3.2

function MyComponent (props) {
  // Use local state.
  const [value, setValue] = useState(initialValue);             #1
  const [state, dispatch] = useReducer(reducer, initialState);  #1
 
  useEffect(() => {
    // Perform side effect.                                     #2
  });
 
  return (
    <p>{value} and {state.message}</p>                          #3
  );
}

Use the listing to compare useState, useReducer, and useEffect within one function component: state setup, side-effect work, and returned UI appear together. section 1.1.3

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Explains rendering updates, side-effect organization, custom-hook extraction, and call-order restrictions, without detailing React internals.
Examples
Demonstrates Quiz, Chat, BookablesList, data fetching, user subscriptions, React Router, Redux, and React Spring integrations.
What it assumes
Assumes basic React and JavaScript knowledge, including components, props, JSX, destructuring, functions, and running React applications.
What you can do after
Apply React Hooks to build stateful function components, organize effects, extract custom hooks, and reuse behavior across components.
How it connects
Connects function components with state, effects, context, custom hooks, event handlers, and class component lifecycle methods.

Also explained in

Compare how 3 books explain React hook →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.