JavaScript React by Example Building the Application Changing Guest Name in State

Gordon Freeman
PRO
Gordon Freeman
Pro Student 825 Points

Should the call-back-funciton always be at the same level Component where the related-State exists?

So the callback function should always be at the same level component where the related-States exist? Can I just put setName callback at the Guest Component? Instead Top level component(where the name State exists) so that I can just pass it down lesser number of times through lesser number of Components

1 Answer

Seth Kroger
MOD
Seth Kroger
Treehouse Moderator 50,598 Points

The short answer is yes. When you consider it, the way React works with data only flowing downward from parent to child the callbacks are the only way to change the the data in state. If you wrote the callback in an intermediate level like Guest, you'd still need another callback to bubble that change up to the component with the state because Guest wouldn't have direct access to App's state. So, rather than write two duplicate callbacks, we write one and pass it all the way down.

Yes, it makes for passing a lot of callbacks though a lot of different levels, but that is the style React gravitates to with one-way binding.

Gordon Freeman
Gordon Freeman
Pro Student 825 Points

That makes sense! Thanks for your time and great answer!