1

I am doing a project where i have a toast function which implements toast there i call the function of fetching data from api and updating my state so that whenever i click the update feed button fetching data from api function called, updation of state and toast of success appears. Now the question is i have a component of categories post displays seperate category post inside of all post component which has the function to display toast, how could i pass the updated state,fetching data from api function from child component that is category post component to parent component that is all post component to implement toast for category component.

1

2 Answers 2

4

If I understand your question correctly -- at a high level, you're trying to figure out how to update a state variable of a parent component from within a child component. Easiest way would be with the useState hook, and then by passing the setState function to the child component.

const ParentComponent = () => {
  const [state, setState] = useState([])

  useEffect(() => {
    // logic that will be executed every time the state variable changes 
  }, [state])

  return <ChildComponent setState={setState} />
}

const ChildComponent = ({setState}) => {
  const handleClick = () => {  
    setState((currentState) => currentState.concat(1))
  }
  
  return <Button onClick={handleClick} />
}

Edit: To answer your question from the comment -- a few things to point out here:

  • You can pass a value to useState which will be the starting value of the variable. In our example, it's an empty array
  • setState has access to the current state, so you can push a value to an array with this syntax: setState((previousState) => previousState.concat(val))
  • useEffect is a hook which is invoked whenever there's a change in the value of the dependency (or dependencies) passed in the second argument. So by including state in its dependency array, we can execute whatever logic we want every time the value of the state variable changes
  • I would also recommend looking into useMemo. It similarly allows you to have aspects of your component logic that are re-executed only when values of certain variables change. For example:
const ParentComponent = () => {
  const [state, setState] = useState([])

  useEffect(() => {
    // logic that will be executed every time the state variable changes 
  }, [state])

  const renderCards = useMemo(() => {
    return state.map(val => <SomeOtherComponent val={val}/>)
  }, [state])

  return (
    <div>
      {renderCards}
      <ChildComponent setState={setState} />
    </div>
  )
}

By wrapping the function inside renderCards in the useMemo hook, the evaluated result is "memoized". And so it won't be executed on every render, unless the variable in the dependency array changes.

Passing down setState to a child component in order to trigger a re-render in the parent component is straightforward when it's an immediate child. If the child component is nested deeper, or there are multiple components that need to react to a change in a variable (e.g. light/dark mode) -- that's when you want to look into a state management tool like Redux or Context.

Sign up to request clarification or add additional context in comments.

3 Comments

Will that updated state remains same on rendering parent component? Let me say i have state of empty array and passing that as props to child component and updating state will that updated state can be used in parent component to implement some functionalities?
Yep. See my updated answer.
@SharathM he said " child data FROM parent component" .... not parent data from child component
0

There are two ways I can think of to achieve what you are trying to do here, i.e. get the child component's state in a parent component.

  1. You can make use of refs. You should be familiar with React hooks to use this approach. The documentation is really good.
  2. You can also make use of a global state using either Redux or Context.

1 Comment

But what i can do with the fetchCategory function which fetches data of categories through api i need to call this function again in parent component to fetch the data again how can i make call of that function in parent component?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.