Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Andrew Folts
Andrew Folts
14,238 Points

React: How to pass top-level methods to grandchild, great-grandchild, etc?

I'm working on a simple project management app.

I have <App> <TasksList> <Task>.

<App> has several task methods (add, delete, etc).

I understand how to pass a method from parent to child, like so:

<Example onClick={...} />

However, what I don't understand is how to pass methods from a grandparent to a grandchild or great-grandchild, etc.

For example, <Task> needs the "deleteTask" method, but its parent <TasksList> doesn't.

1 Answer

jared eiseman
jared eiseman
29,023 Points

There's ways to circumvent having to pass it through the "middle-man" so to speak through libraries like Redux. However for a simple app, might be overkill... It sounds like you have a grasp of the pattern with passing the method as a prop through TasksList to Task, and you're right on the money there. Even if TasksList doesn't need it. Unless of course, you want to go the route of using a library like Redux or Flux or what have you.

Andrew Folts
Andrew Folts
14,238 Points

I figured that would be the case. I'll probably dive into Redux or Flux, because I know the app is going to get more complex, eventually. Thanks for the tip!