What is Event Handling in React? Complete Beginner's Guide (2026)

Image
  What is Event Handling in React? Event Handling in React is the process of responding to user actions by executing JavaScript functions. Whenever a user interacts with a React application—such as clicking a button, typing in a text box, submitting a form, or moving the mouse—React detects that action and calls a function to perform the required task. Whenever a user clicks a button, types in an input box, submits a form, moves the mouse, or presses a keyboard key, React detects that action and executes a function. This process is called Event Handling. For example, when a user clicks a Login button, React can validate the entered username and password. When a user types in a search box, React can display matching search results immediately. Without event handling, these interactions would not be possible, and the application would remain static. Basic Syntax of Event Handling function App() {     function showMessage() {     alert("Welcome to React!"); ...

Component Communication in React: Complete Beginner's Guide (2026)

 

What is Component Communication in React?

React Component Communication Tutorial with Examples


Introduction

Component Communication is the process of sharing data, functions, and events between React components so they can work together, respond to user interactions, and keep the application's user interface updated.

When you build a React application, you don't create everything inside a single file. Instead, you divide the application into small, reusable components. Each component has its own responsibility. For example, one component may display a navigation menu, another may show a list of products, while another handles the shopping cart or a login form.

Although these components are independent, they still need to work together. A product component must inform the shopping cart when a user adds an item. A search box must send the entered keyword to the product list so it can display matching results. A login form must notify the main application after a successful login.

The process that allows these components to exchange information is called Component Communication.

Component Communication is one of the core concepts of React because it connects individual components and enables them to function as a complete application. Without it, every component would work in isolation, making it impossible to build interactive websites or web applications.


Definition

React follows a predictable data flow. Information generally moves from a parent component to a child component using props, while child components notify their parent through callback functions. When multiple components need the same information, developers can share state through a common parent or use the Context API.


Understanding Component Communication

Imagine you are building an online shopping website.

The application contains several components:

  • Header

  • Product List

  • Product Card

  • Shopping Cart

  • Checkout Page

These components are responsible for different tasks, but they still depend on one another.

When a customer clicks the Add to Cart button, several things happen:

  • The Product Card identifies which product was selected.

  • The parent component updates the cart information.

  • The Shopping Cart displays the new item.

  • The Header updates the cart count.

  • The Checkout page recalculates the total amount.

All of these updates happen because the components communicate with one another. If communication did not exist, the shopping cart would never know that a product had been added.


Why Do We Need Component Communication?

Every React application stores and displays information such as user details, product lists, form values, notifications, or application settings. This information often needs to be shared across different parts of the application.

Component Communication helps developers:

  • Share information between components.

  • Keep the user interface synchronized with application data.

  • Respond to user actions immediately.

  • Reuse components with different data.

  • Build applications that are organized and easy to maintain.

Without communication, developers would have to duplicate data in many places, leading to inconsistent information and making applications difficult to manage.


How Component Communication Works

React follows a simple communication cycle:

  1. A parent component stores data.

  2. The parent passes that data to a child component.

  3. The child displays the information.

  4. The user performs an action, such as clicking a button or entering text.

  5. The child informs the parent about the action.

  6. The parent updates its state.

  7. React automatically refreshes the affected components.

This process keeps the application's interface synchronized with the latest information.

Component Communication in React – Syntax Explained

In React, components communicate with each other to share information and respond to user actions. The syntax used for communication depends on the direction in which the data flows. The most common communication patterns are:

  • Parent to Child (using Props)
  • Child to Parent (using Callback Functions)
  • Communication between Multiple Components (using shared state)

Let's understand the basic syntax of each communication method.


1. Parent to Child Communication Syntax

A parent component sends information to a child component by passing props.

Syntax

function Parent() {

  const message = "Welcome to React";

   return (

    <Child text={message} />

  );

}

 function Child(props) {

  return <h2>{props.text}</h2>;

}

Explanation

Let's understand the syntax step by step.

Step 1: Create Data

const message = "Welcome to React";

The parent component creates a variable named message. This is the information that will be shared with the child component.


Step 2: Pass Data as a Prop

<Child text={message} />

The parent renders the Child component and passes the value of message using a custom attribute named text.

Here:

  • text is the prop name.
  • message is the value being passed.

Step 3: Receive the Prop

function Child(props)

The child component receives all the props inside the props object.


Step 4: Display the Data

<h2>{props.text}</h2>

The child accesses the value using props.text and displays it on the screen.


Component Communication in React Data Flow

Parent Component

      |

Creates Data

      ⬇

Passes Props

      ⬇      

Child Component

      

Displays Data

This is called one-way data flow because the data moves only from the parent to the child.

what are Children props in React


2. Child to Parent Communication Syntax

A child component cannot directly modify its parent. Instead, the parent passes a function to the child. The child calls that function whenever it needs to send information back.

Syntax

function Parent() {

   function handleClick() {

    alert("Button Clicked");

  }

   return (

    <Child onButtonClick={handleClick} />

  );

}

 function Child(props) {

   return (

    <button onClick={props.onButtonClick}>

      Click Here

    </button>

  );

}

Explanation

Step 1: Create a Function

function handleClick() {

  alert("Button Clicked");

}

The parent creates a function that performs an action.

Step 2: Pass the Function

<Child onButtonClick={handleClick} />

The function is passed to the child as a prop.


Step 3: Receive the Function

function Child(props)

The child receives the function inside the props object.


Step 4: Call the Function

<button onClick={props.onButtonClick}>

When the user clicks the button, the child calls the parent's function.

The parent then performs the required action.


Component Communication in React 

Communication Flow

Parent Component

       ⬇

Creates Function

       

Passes Function

       

Child Component

       

User Clicks Button

       

Function Executes

       

Parent Responds


3. Passing Data Back to the Parent

Sometimes the child needs to send a value back instead of simply calling a function.

Syntax

function Parent() { 

  function receiveMessage(message) {

    alert(message);

  }

   return (

    <Child sendMessage={receiveMessage} />

  );

}

 function Child(props) {

   return (

    <button

      onClick={() => props.sendMessage("Hello Parent")}

    >

      Send Message

    </button>

  );

}

Explanation

The child calls the parent's function and provides a value.

props.sendMessage("Hello Parent")

The parent receives that value.

function receiveMessage(message)

The variable message now contains:

Hello Parent

What are Default props in React


Component Communication in React:  Flow

Child

   ⬇

Calls Parent Function

    

Sends Data

    

Parent Receives Data

   

Updates Application

4. Updating Parent State

In most React applications, the child requests the parent to update its state.

Syntax

import { useState } from "react"; 

function Parent() { 

  const [count, setCount] = useState(0); 

  function increaseCount() {

    setCount(count + 1);

  }

   return (

    <>

      <h2>{count}</h2>

      <Child increase={increaseCount} />

    </>

  );

}

 function Child(props) {

   return (

    <button onClick={props.increase}>

      Increase

    </button>

  );

}


Explanation

The parent stores the value in state.

const [count, setCount] = useState(0);

The child never changes count directly.

Instead, it calls:

props.increase()

The parent updates the state.

setCount(count + 1);

React automatically refreshes the displayed value.

Complete Communication Flow

Parent Component

    ⬇ 

Creates State

     ⬇  

Passes Data or Function

     ⬇  

Child Component

     ⬇  

User Interaction

     ⬇   

Child Calls Function

     ⬇  

Parent Updates State

    ⬇ 

React Re-renders UI

Key Points to Remember

  • A parent sends data to a child using props.
  • A child communicates with a parent by calling a callback function.
  • React follows one-way data flow, where data moves from parent to child.
  • State should be updated in the component that owns it.
  • When the state changes, React automatically updates the user interface.

Real-World Example

Consider a banking application.

A customer transfers money to another account.

The transfer form sends the transaction details.

The account balance updates automatically.

The transaction history immediately displays the new transaction.

The notification component shows a success message.

Although each feature belongs to a different component, they all communicate to complete a single task. This is exactly how Component Communication works in React.


Component Communication Diagram

The following diagram shows the basic relationship between a parent component and a child component.

                                 React Application
                                                 |
                                                 |
                               Parent Component
                                                 |
| ┌──────────────────────────────┐
| |
| | Pass Data (Props) |
⛛ ⛛
Child Component Send Action (Callback)
| |
└───────────────-───────────────┘ |
Parent Updates State ⬇
React Re-renders the UI

Diagram Explanation

  • The Parent Component owns the data or state.
  • It passes information to the Child Component using props.
  • The user interacts with the child component, such as clicking a button.
  • The child cannot directly change the parent, so it calls a callback function.
  • The parent updates its state.
  • React automatically refreshes the affected components and displays the updated information.

Parent to Child Communication Diagram

The most common communication pattern in React is passing data from a parent component to a child component.

      Parent Component
             |
|
Creates Data
⬇ Passes Props ⬇

Child Component

Displays Data

Explanation

The parent component stores the required information and sends it to the child. The child receives the data through props and displays it without modifying the original value.

Child to Parent Communication Diagram

Sometimes a child component needs to notify the parent about an event.

      Parent Component

Creates Callback Function
|

Child Component
|
User Performs Action
|

Calls Parent Function
|
Parent Updates State |
Updated Data Displayed

Explanation

Instead of changing the parent's data directly, the child calls a function that the parent has provided. The parent then updates its own state and React refreshes the interface.

Multiple Components Communication Diagram

In many applications, several components need access to the same information.

                 Parent Component
                            |
Shared Application State
| ┌─────────────────┐
| | Header Product List Cart
| | |
└─────────────────┘ |
Updated Information

Explanation

The parent stores the shared state and distributes it to all related child components. Whenever the shared state changes, every component that depends on it automatically receives the latest data.

Component Communication Flowchart

The following flowchart explains how communication happens inside a React application.

               Start
|
Create Parent Component
|


Store Data in State |


Pass Data to Child Using Props
                   |

Child Displays Information
|
User Performs an Interaction
|
Child Calls Parent Callback Function
|
Parent Updates Its State
|
React Detects State Change |
React Re-renders Components
|
Updated Information Appears
|
⛛ End

Complete Communication Cycle

The communication process in React can be summarized as follows:

Application Starts
        
                  |
Parent Stores Data
                  |
Data Passed as Props
                  |
Child Receives Data
  
|

User Clicks or Types
                  |
Child Calls Parent Function
                  |

Parent Changes State
                  |
React Updates the Interface

Everyday Analogy

Think of a hospital.

  • The receptionist registers the patient.

  • The doctor receives the patient's information.

  • The laboratory receives test requests.

  • The pharmacy receives the prescribed medicines.

Each department performs a different job, but all departments exchange information to provide proper treatment.

React components work in a similar way. Each component has its own responsibility, but communication allows them to function as one complete application.

Benefits of Component Communication

Component Communication offers several important advantages:

  • It allows components to exchange information without duplicating data.

  • It keeps the user interface updated whenever information changes.

  • It improves code reusability by allowing the same component to display different data.

  • It encourages developers to build smaller, focused components.

  • It makes applications easier to understand, maintain, and expand.

  • It supports the development of complex applications without losing organization.

 Component Communication Used

You will find Component Communication in almost every React application, including:

  • Online shopping websites

  • Banking applications

  • Social media platforms

  • Learning management systems

  • Food delivery applications

  • Hospital management systems

  • Employee management software

  • Dashboard applications

  • Chat applications

  • Online examination systems

Whenever two or more components need to exchange information, Component Communication is involved.

component Communication in React – FAQ (Frequently Asked Questions)

1. What is Component Communication in React?
Component Communication is the process of sharing data, functions, or events between React components. Since React applications are built using multiple reusable components, they often need to exchange information to work together.

2. Why is Component Communication important?

Without component communication, components would work independently and could not share data. It allows developers to create dynamic, interactive, and reusable applications.



3. What are the main ways components communicate in React?
  • The most common methods are:
  • Parent to Child using Props
  • Child to Parent using Callback Functions
  • Sibling to Sibling through a Common Parent
  • Global communication using Context API
  • Large-scale state management using Redux, Zustand, or other state libraries

4. What are Props in React communication?
Props (Properties) are read-only values passed from a parent component to a child component. They allow the parent to send data to its children.

5. Can a child component modify Props?
No. Props are immutable (read-only). A child component can use them but cannot directly change their values.

6. How does a child component send data to its parent?
A child component calls a function passed by the parent as a prop. This callback function sends data or events back to the parent.
7. How do sibling components communicate?
  1. Sibling components cannot communicate directly. Instead:
  2. One sibling sends data to the parent.
  3. The parent updates its state.
  4. The updated data is passed to the other sibling through props.

8. What is Prop Drilling?
Prop Drilling happens when data must be passed through several intermediate components just to reach a deeply nested child. This can make code difficult to maintain.
9. How can Prop Drilling be avoided?
  1. You can reduce or eliminate prop drilling by using:
  2. Context API
  3. Redux
  4. Zustand
  5. Recoil
  6. Jotai
  7. These solutions allow components to access shared data without passing props through every level.
10. What is Context API?
Context API is a built-in React feature used to share data among multiple components without manually passing props through each level.

11. When should Context API be used?
  1. Context API is useful when multiple components need access to the same data, such as:
  2. User information
  3. Theme (Light/Dark Mode)
  4. Language settings
  5. Authentication status
12. What is state lifting?
State lifting means moving state from child components to their closest common parent. The parent manages the state and passes data to children through props.

13. What is one-way data flow in React?
React follows one-way (unidirectional) data flow. Data moves from parent components to child components through props, making applications easier to understand and debug.
14. Can two unrelated components communicate directly?
No. Unrelated components usually communicate through:
A common parent
Context API
Global state management libraries
15. Which communication method is best?
There is no single best method.
Props → Small applications
Callback Functions → Child-to-parent communication
Context API → Medium applications with shared data
Redux/Zustand → Large applications with complex state

React Component Communication Interview Questions 

1. What is Component Communication in React?
Component Communication is the process of exchanging data and events between React components so they can work together. It allows different parts of the application to share information and respond to user interactions.

2. Why do React components need communication?

Components often depend on each other. Communication enables them to:

Share data

Handle events

Update the user interface

Keep the application synchronized

3. How does a parent component send data to a child component?
A parent sends data using Props.

4. How can a child component send data to its parent?
A child component calls a callback function received through props. The parent defines the function and receives the data when the child invokes it.
5. What are Props?
Props are read-only inputs passed from a parent component to a child component. They help make components reusable and configurable.

6. Why are Props immutable?

React keeps props immutable to maintain predictable data flow. Only the parent component should control and update the values it passes down.

7. Explain one-way data binding in React.
React uses one-way data binding, meaning data flows only from parent to child. This approach makes applications easier to debug, test, and maintain because the direction of data is always clear.

8. What is state lifting?
State lifting is the practice of moving shared state to the nearest common parent so multiple child components can access and update the same data consistently.

9. What is Prop Drilling?
Prop Drilling occurs when props are passed through multiple intermediate components that do not use the data themselves, only to deliver it to a deeply nested component.

10. How can Prop Drilling be solved?

Common solutions include:
Context API
Redux
Zustand
Recoil
Jotai
These tools provide shared state without passing props through every intermediate component.

11. What is Context API?
Context API is React's built-in mechanism for sharing data across many components without manually passing props at every level of the component tree.

12. When should you use Context API instead of Props?
Use Context API when the same information needs to be accessed by many components throughout the application, such as authentication details, themes, or language preferences.

13. Can sibling components communicate directly?
No. Sibling components communicate indirectly through their common parent or by using shared state with Context API or another state management solution.

Comments

Popular posts from this blog

HTML Tag

CSS Text Color Explained with Syntax and HTML Examples

HTML Input Type Submit Syntax and Example