#How can i pass props from parent to child at the second level and back?

4 messages · Page 1 of 1 (latest)

hot ore
#

Instead of prop drilling the setter function down to the CommentView you could try passing the value back up to component where the state is defined and set it there.

const Parent = () => {
  const [height, setHeight] = useState()

  const handler = (calculatedHeight) => {
    setHeight(calculatedHeight)
  } 

  return <child
      onCalculateHeight={handler}
      height={height}
    />
}

You could also look into creating a context, however that has some caveats worth considering before choosing that over prop drilling

strong sorrel
#

@hot ore Thank you for your reply, I will look into this, Context seems easier, but in the case of prop drilling, it didn't work, no matter how, i defined the functions in the parent and sent them down as props until the lowest component, CommentView.

So, useContext might be just what I need.

strong sorrel
#

@hot ore I just realized, i was approaching the whole thing from the wront direction. I need to send data from CommentView componenet to the Layout so that it can render correctly my design. i need to define states in CommentView and send them upwards the com[ponenet tree.

But there's a problem with the layout componenet. I cannot call the layout componenet inside th GetPost and commentView componenets, because that will screw up my website deisgn-wise.

strong sorrel
#

i don't know about this, but either way i still get that the defined stateupdater functions are not functions error