JavaScript React Components Managing State and Data Flow Communicating Between Components

Luke Markham
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Luke Markham
Front End Web Development Techdegree Graduate 17,287 Points

Identifying the callback function

I'm having trouble understanding exactly how this is working.

To my current understanding we:

1) created the function handleRemovedPlayer()

2) passed it down to <Player /> via a prop call removePlayer

3)on clicking the button in Player we invoke an annoymous function, which then invokes the function handleRemovedPlayer() that is held in the prop removePlayer

How does removePlayer suddenly become a callback function ? isn't it just the prop that represents handleRemovedPlayer()

Is removePlayer thought of as a callback because Player itself is a function ?

1 Answer

David Austin
David Austin
1,217 Points

Hi Luke -

Fellow student here, but I figured that explaining this may help others as well and it helps me to further understand by diving deeper into what is happening. Take a look at the graphic I provided below (credit to Jason Arnold, a Medium author)! After reading further into it, I think you are just coming across a common issue of interpretation. Parent components can communicate to child components with props. But when it is time to have children communicate back, they use the "callback function". The purpose of that is to change a piece of state that is part of the parent component. Remember: props are read-only or "pure" because they don't change their inputs.

Hope I could help!

https://medium.com/@thejasonfile/callback-functions-in-react-e822ebede766

Luke Markham
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Luke Markham
Front End Web Development Techdegree Graduate 17,287 Points

Hey David! You're exactly right on the teaching to learn a thing! Actually, I read the medium article not long after watching this video! Thank for your reply anyways!