How to pass data to child component in react js

In this blog post we will describe how you can pass some data from one component to another component or parent component to child component.

Lest create two component one is parent.js and second one is child.js, you can give any name to these components.

After creating these file on src folder, lets check all files code and try to understand whats happening inside the code.

Index.js File

import React from ‘react’;
import ReactDOM from ‘react-dom’;
import ‘./index.css’;
import App from ‘./App’;
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById(‘root’)
);

App.js File

import ‘./App.css’;
import Parent from ‘./Parent’;
function App() {
  const parentData = “passing data to parent component”;
  const childData = “passing data for child component”;
  return (
    <div className=“App”>
      <Parent sendParentData={parentData} sendChildData={childData}/>
    </div>
  );
}
export default App;

Parent.js File

import React from ‘react’;
import Child from ‘./Child’;
const Parent=(props)=>{
    console.log(props);
    return (
        <>
        <h1>{props.sendParentData}</h1>
        <Child childData={props.sendChildData}/>
        </>
    )
}
export default Parent;

Child.js File

const Child=(props)=>{
    return (
        <h1>{props.childData}</h1>
    )
}
export default Child;

Let’s understand what’s happening here.

Inside Index.js file we are calling App.js file in JSX, so App file data will appear first when project gets load.

Inside App.js file we are creating two variables with some data to display for parent and child component files.

  const parentData = “passing data to parent component”;
  const childData = “passing data for child component”;

We are also importing Parent.js file and calling inside the App.js file JSX code.

 <Parent sendParentData={parentData} sendChildData={childData}/>

These sendParentData and sendChildData are properties which we are passing to parent component.

Inside Parent.js file we are recieving these values inside the props variable, this props variable is a object of all these properties those we are sending in App.js file by passing inside the Parent tag.

This props variable has data in object format like below

{
 sendParentData: ‘passing data to parent component’,
 sendChildData: ‘passing data for child component’ 
}

so we can easily get this object data and display in html by the help of data binding.

 return (
        <>
        <h1>{props.sendParentData}</h1>
        <Child childData={props.sendChildData}/>
        </>
    )

Simmilar we are doing with Child component and passing data which is coming from the app component file and then passed to child component from the parent component.

Now in simmilar way we can get this data using props and display in child component JSX.

 return (
        <h1>{props.childData}</h1>
    )

This way we can pass data from one component to another component so this communication is called parent to child communication as we are importing components and passing data to them.

Lets check the output for this code.

As we are importing parent component in app component and inside parent component we are importing child component so both component will display on same page which is app component JSX output.

For any question and doubts you can comment below and also if you want a blog on specific topic please feel free to comment.

Related Posts

angular vs react-min

What is the difference between Angular & React JS, Which one you should choose?

Angular & React JS both are use for Frontend Development, To make Single page applications or Web Apps. Here is a quick difference between both of them….

react js icon

What is Redux? What is action, reducer and store in Redux

In this blog we will discus about the redux, what is Redux and how you can use it in React application. What is Redux? Redux is a…

react js icon

What is useEffect Hook? How to Fetch data from API using useEffect

useEffect hook is used to manage side effects that are not related to component rendering. Mostly useEffect hook is used for managing console logs, animations, loading data,…

react js icon

What is useReducer Hook? What is the difference between useState and useReducer Hooks?

useReudcer hook takes the current state and returns a new state, it is used for state management. With useReducer, we can directly call the function inside the…

react js icon

Creating React JS App without using npx create-react-app command

It’s easy to create a react application by just running npx create-react-app project_name command, do you know what are the basic requirements for creating a react application…

react js icon

What is the useState() hooks in React JS | How to use useState() hook in React JS

Hooks are very important in react js and the most common hook is the useState Hook. In this blog, we will look into the functionality of this…

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *