Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass particular property to all child components in react?

I am absolute beginner in react. I have to pass particular property to all the components inside the div without passing them into individual, For Example, Instead of doing this:

 function App() {
    return (
      <div>
        <Component1 props = {propObject}/>
        <Component2 props = {propObject}/>
        <Component3 props = {propObject}/>
      </div>
    );
}

How can I achieve something like this? :

function App() {
  return (
    <div props={propObject}>
      <Component1 />
      <Component2 />
      <Component3 />
    </div>
  );
}
like image 239
krupal_m Avatar asked Dec 19 '21 06:12

krupal_m


People also ask

How do you pass all props to child component React?

Instead, to pass all React props from parent to child at once, you need to take advantage of the spread operator ( ... ). The spread operator lets you pass the entire props object to a child component as that child's props object.

How do you pass a property to a component in React?

You can pass data in React by defining custom HTML attributes to which you assign your data with JSX syntax. So don't forget the curly braces. }export default App; As you can see, the props are received in React's class component via the this instance of the class.

How do I pass multiple data from parent to child in React?

First, you'll need to create two components, one parent and one child. Next, you'll import the child component in the parent component and return it. Then you'll create a function and a button to trigger that function. Also, you'll create a state using the useState Hook to manage the data.


1 Answers

React Context

provides a way to share values to child components without having to explicitly pass a prop through every level of the tree.

You need to import React.context from react

syntax:

// context.js

import React, { createContext } from "react"

export const MyContext = React.createContext(null);

Now You need to create a provider of this context, to pass the current context value to the tree below. So you need to wrap all of your child component by the React.Provider.

syntax:

// MainComponent.js
import { MyContext } from "./context"
import { ChildComponent1, ChildComponent2, ChildComponent3 } from "./childcomponent" 

const MainComponent = () => {
  const myObject = {name: "John"}

  return (
    <MyContext.Provider props={myObject}>
      <ChildComponent1 />
      <ChildComponent2 />
      <ChildComponent3 />
    </MyContext.Provider>
  )
}

Now all the child component of this MainComponent has access of this props value which is provided by MyContext.Provider. Now you can simply get that values to your child components.

syntax:

// childcomponent.js
import React, { useContext } from "react"
import { MyContext } from "./context"

export const ChilcComponent1 = () => {
  const props = useContext(MyContext)
  return (
    <p>
      My name is: {props?.key}
    </p>
  )
}
like image 99
Pradip Dhakal Avatar answered Oct 19 '22 00:10

Pradip Dhakal