React What is JSX in React? Features, Advantages, Working & Example
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.
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.
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.
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.
React follows a simple communication cycle:
A parent component stores data.
The parent passes that data to a child component.
The child displays the information.
The user performs an action, such as clicking a button or entering text.
The child informs the parent about the action.
The parent updates its state.
React automatically refreshes the affected components.
This process keeps the application's interface synchronized with the latest information.
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:
Let's understand the basic syntax of each communication method.
A parent component sends information to a child component by passing props.
Syntax
function Parent() {
const message = "Welcome to React";
<Child text={message} />
);
}
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.
<Child text={message} />
The parent renders the Child component and passes the value of message using a custom attribute named text.
Here:
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.
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
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() {
alert("Button Clicked");
}
<Child onButtonClick={handleClick} />
);
}
<button onClick={props.onButtonClick}>
Click Here
</button>
);
}
Step 1: Create a Function
function handleClick() {
alert("Button Clicked");
}
The parent creates a function that performs an action.
<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);
}
<Child sendMessage={receiveMessage} />
);
}
<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
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);
}
<>
<h2>{count}</h2>
<Child increase={increaseCount} />
</>
);
}
<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
Key Points to Remember
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.
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 UIThe 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 DataSometimes 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 DisplayedInstead 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.
In many applications, several components need access to the same information.
Parent Component
|
Shared Application State
|
┌─────────────────┐
| |
Header Product List Cart
| | |
└─────────────────┘
|
Updated InformationThe 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.
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
|
⛛
EndThe 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 InterfaceThink 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.
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.
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.
3. How does a parent component send data to a child component?
A parent sends data using Props.
Comments
Post a Comment