Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to pass multiple props down to n children

Currently working a group project and I've been looking over the code the group wrote and I'm curious if there is a better way to pass props to n children components that change the state of the top-level App component.

currently we have something like this:

return (
  <div>
    <header className="navBar">
      // NavBar is an example for the question
      <NavBar
        showSplash={this.state.showSplash}
        displayAllSearchResults={this.displayAllSearchResults}
        searchBarDisplay={this.state.showCard}
        updateResults={this.updateResults}
        selectResult={this.selectResult}
        searchResults={this.state.searchResults}
        showSuggestions={this.state.showSuggestions}
      />
    </header>
    <section className="App">
      <article className="mainContent">{card}</article>
    </section>
    <div className="appBackground" />
  </div>
);

All of these methods (which there are probably too many) are being passed into the NavBar component that itself is composed with a SearchBar component which also needs that giant block passed into it in order to change the state all the way back at the top-level App. This feels wrong, is there a solution or better approach to passing all these props down?

like image 931
kevin Avatar asked Apr 07 '19 22:04

kevin


People also ask

What is the best way to send 4 or more props to a child component?

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 multiple props individually to a component?

Method 1: We can make a separate method for the event and call all the props method in that method. Method 2: We can create an anonymous function and call all the props method inside the anonymous method.


2 Answers

You can use the spread operator like this :

<NavBar  {...this.state} />

It will pass you state properties as props to the navbar.

In you Navbar component, you can get the value of showSplash in state from props.showSplash

If you want to pass the data into a child component of Navbar, you can do something like this :

<ChildComponent {...props} /> // if Navbar is a functional component

<ChildComponent {...this.props} /> // if Navbar is a stateful component

Reference:

Spread in object literals

Here is a sample stackblitz for illustration.

like image 155
Abdelkarim EL AMEL Avatar answered Nov 15 '22 15:11

Abdelkarim EL AMEL


If you want a data to be accessible by all the child components, then you have following options:

  • React Context
  • Redux or state management library like flux etc.

Or you could follow @Abdelkarim EL AMEL answer if that soughts out you issue for now.I would suggest take a look at the above mentioned options and if setting a global state which can be accessed anywhere in the app helps you to not send prop to each component explicitly. I would prefer to use a global state for variable which are common and needs to be accessed by all the child of the root app.

For example a button component to change the theme of the app. It changes the state of the root app and is accessible by every component.

like image 40
Deepak Adhana Avatar answered Nov 15 '22 16:11

Deepak Adhana