liveBook · Manning

Identity

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

Function identity is the property by which a function value is distinguished from other function values, including whether it remains the same value across renders.

Function identity distinguishes one function value from another. A function defined inside a component is recreated on every render, so a child receiving it as a prop can observe a new value even when its behavior is unchanged. section 6.5

Identity matters when functions participate in rendering or effect dependencies:

  • Updater functions returned by useState or useReducer have stable identity. section 6.5.1
  • Custom updater functions can change identity on every parent render, causing dependent effects to run again. section 6.5.1
  • useCallback returns the same function until one of its dependencies changes. section 6.5.2

Preventing repeated data fetches

BookablesView passes a custom updateBookable function to BookablesList. Without memoization, selecting the first fetched bookable re-renders the parent, creates a new function, retriggers BookablesList’s useEffect, and repeats the fetch. Wrapping the handler in useCallback with an empty dependency list keeps the prop stable. section 6.5.1 section 6.5.2

Listing 6.17 Maintaining a stable function identity with useCallback
import {useState, useCallback, Fragment} from "react";                    #1
 
import BookablesList from "./BookablesList";
import BookableDetails from "./BookableDetails";
 
export default function BookablesView () {
  const [bookable, setBookable] = useState();
 
  const updateBookable = useCallback(selected => {                        #2
    if (selected) {
      selected.lastShown = Date.now();
      setBookable(selected);
    }
  }, []);                                                                 #3
 
  return (
    <Fragment>
      <BookablesList bookable={bookable} setBookable={updateBookable}/>   #4
      <BookableDetails bookable={bookable}/>
    </Fragment>
  );
}
#1 Import the useCallback hook.
#2 Pass the updater function to useCallback.
#3 Specify the dependencies.
#4 Assign the stable function as a prop.

Shows importing useCallback, memoizing the updater, declaring dependencies, and passing the stable function as a prop. section 6.5.2

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

How deep it goes
Explains render-time redefinition, dependency comparison, and useCallback stabilization through a worked fetch-loop example.
Examples
Demonstrates selected bookables, custom validation, timestamp enhancement, repeated network requests, and a useCallback-based updater.
What it assumes
Assumes working knowledge of React components, props, state, effects, dependency lists, and JavaScript functions.
What you can do after
Apply it via useCallback to keep function props stable and prevent unintended effect reruns.
How it connects
Connects function identity with React props, effects, state updaters, reducers, memoization, and memoized components.

Also explained in

Compare how 8 books explain identity →

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.