Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pass down state to child components with React Hooks?

I'm trying to re-write some of my code in React Hooks, and I was wondering, when I pass down searchData in this example... Will the now have access to searchData and setSearchData? I'm confused about how passing around state in React Hooks works.

import React, { useState, useEffect } from "react";
import SearchBarContainer from "../SearchBar/SearchBarContainer";
import PostContainer from "./PostContainer";



function PostPage(props) {
   const [searchData, setSearchData] = useState([...props.data]);
   const [addLike, setAddLike] = useState([]);

 useEffect(() => {
 setAddLike({ addLike: Array(props.data.length).fill(false) });
  });

return (
  <div>
    <SearchBarContainer data={props.data} searchData={searchData} />

    <div css={parentPostContainer}>
      {searchData.map((dataOnMap, index) => {
        return (
          <PostContainer
            data={dataOnMap}
            addLike={addLike[index]}
            searchData={searchData}
          />
        );
      })}
     </div>
   </div>
 );
}

export default PostPage;
like image 520
Bradley Ball Avatar asked Jan 18 '19 20:01

Bradley Ball


People also ask

How do you pass data to child component React hooks?

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.

Do hooks share state between components?

If you are referring to component state, then hooks will not help you share it between components. Component state is local to the component. If your state lives in context, then useContext hook would be helpful.

Can you pass state between components React?

Props: It allows you to pass data from a parent component to a child component. State: While props allow you to pass data from a parent component to a child component, the state is used to change the component, well, state from within.


2 Answers

Yes, as far as passing down props are concerned there is no difference in usage with React Hooks. In the snippet you've provided just pass setSearchData as a prop to PostContainer.

<PostContainer
  data={dataOnMap}
  addLike={addLike[index]}
  searchData={searchData}
  setSearchData={setSearchData}
/>
like image 181
Deniz Aslan Avatar answered Oct 13 '22 11:10

Deniz Aslan


Yes Absolutely you can. In fact React docs talk about "Lifting state up" so to speak.

If there is shared state between components and you need to lift it up to their nearest ancestor.

The general concept is props are passed down and state is passed up.

However, this can get messy. If you are working on a larger project, I would recommend using Redux.

You can pass the state as props, but if you want child components to alter state, then you can pass the setter from the useState directly to child components.

Here is the same example:

<PostContainer
   data={dataOnMap}
   addLike={addLike[index]}
   searchData={searchData}
   setSearchData={setSearchData}
   setAddLike={setAddLike}
/>

Or another solution

const LikeManager = {
  addLike: setAddLike,
  data: {
     //some data
  }  
}

<PostContainer
   data={dataOnMap}
   likeManager: {LikeManager}
/>

I know this is not part of the question, but I would recommend using scalar values for useState wherever possible

like image 41
ptmp_727 Avatar answered Oct 13 '22 09:10

ptmp_727