Callback Handlers and Lifting State in React – Component Communication and Shared Data Management

In React, props allow data to flow from parent to child, but not the other way around. To enable upward communication, we use callback handlers — functions passed down as props and invoked in child components. This article explains how to define and use callback handlers, and introduces the concept of lifting state to manage shared data at the appropriate component level.

callback-handlerlifting-statedata filteringcomponent communication

~2 min read · Updated Oct 22, 2025

Introduction


In React, props are used to pass data from parent to child components. But to communicate from child to parent, we use callback handlers. These are functions defined in the parent, passed down via props, and invoked in the child to send data or trigger actions upward.


Defining a Callback Handler


In the example below, the App component defines a handleSearch function and passes it to the Search component:


const App = () => {
  const stories = [ ... ];
  const handleSearch = (event) => {
    console.log(event.target.value);
  };

  return (
    
); };

In the Search component, the function is received via props and called inside the input’s change handler:


const Search = (props) => {
  const [searchTerm, setSearchTerm] = React.useState('');

  const handleChange = (event) => {
    setSearchTerm(event.target.value);
    props.onSearch(event);
  };

  return (
    
); };

Lifting State


If the parent component needs access to a piece of state, it’s better to define that state in the parent. In this case, we move searchTerm from Search to App:


const App = () => {
  const stories = [ ... ];
  const [searchTerm, setSearchTerm] = React.useState('');

  const handleSearch = (event) => {
    setSearchTerm(event.target.value);
  };

  return (
    
); };

Filtering Data with State


Now we can use searchTerm to filter the stories before passing them to the List component:


const searchedStories = stories.filter((story) =>
  story.title.includes(searchTerm)
);

Then pass the filtered list:


<List list={searchedStories} />

Conclusion


Callback handlers allow child components to communicate with parents. Lifting state ensures shared data is managed at the right level. Together, these patterns make React components more maintainable, scalable, and interactive.


Written & researched by Dr. Shahin Siami

Related Articles

Forms in React – Submitting Data with onSubmit and Action in Search Forms

react-forms-Forms are essential in modern applications. This article walks through building a search form in React using input and button elements. First, we use the onSubmit attribute to manage form submission, then introduce the action attribute to align with native HTML behavior. This structure improves accessibility, readability, and prepares the app for future React updates.submit-search-with-onsubmit-and-action

Continue

Third-Party Libraries in React – Using Axios and Async/Await for Data Fetching

This article explores how to replace the native fetch API with the more stable and feature-rich Axios library for data fetching in React. It also demonstrates how to refactor promise-based code using async/await and try/catch blocks for improved readability and error handling. These techniques enhance compatibility with older browsers, improve testability, and simplify asynchronous logic.

Continue

Explicit Data Fetching in React – Controlling API Requests with a Confirmation Button

In the initial implementation, every keystroke in the search field triggered a new API request. This article demonstrates how to shift to explicit data fetching in React, where data is only fetched when the user clicks a confirmation button. By separating the searchTerm and url states, and using useCallback and useEffect, we gain full control over when API requests are sent.

Continue

Impossible States in React – Predictable State Management with useReducer

Using multiple useState hooks to manage related states like data, loading, and error can lead to impossible states — combinations that should never occur in a well-designed UI. This article shows how to consolidate related states into a single useReducer hook, enabling predictable transitions and eliminating bugs like simultaneous loading and error indicators. By structuring state as a complex object and dispatching clearly defined actions, we gain control and clarity in asynchronous data flows.

Continue

Advanced State Management in React – Using useReducer for Complex Transitions

While useState is ideal for simple state updates, React’s useReducer hook offers a more scalable solution for managing complex state transitions. This article demonstrates how to migrate story-related state from useState to useReducer, handle multiple actions like setting and removing stories, and refactor reducer logic for clarity. It’s a foundational step toward building robust, declarative state logic in React applications.

Continue

Conditional Rendering in React – Displaying Loading and Error States for Asynchronous Data

Conditional rendering in React is a powerful tool for controlling what appears in the UI based on different states. This article explains how to show a “Loading…” message while asynchronous data is being fetched, and how to display an error message if the request fails. Using useState and useEffect, we manage isLoading and isError flags, and render content dynamically using ternary and logical operators.

Continue