What does this.props.onChange(name) does in handleChange , it is not a function anywhere in the other files . Thanks !
I’m not sure which exercise this came from, so I can’t point out specific parts of code, but I can still give you a quick overview so you can find it.
this.props
is what you use to access the information that was passed to the component using the attributes in JSX. The name of the prop
is set by this attribute too, so even if you don’t have a method named onChange()
somewhere, it means that onChange
was the name of the prop
when the Child component was used.
Look for whatever component is using Child. It will include something like this in the render()
method:
<Child onChange={this.methodNameHere} />
From there, you’ll look for whatever method was being passed to Child using the onChange
attribute.
I think the screenshot comes from the exercise:
https://www.codecademy.com/courses/react-101/lessons/child-updates-parents-state/exercises/child-parent-define-handler
I think you’re right.
@gigawhiz85773 with this new information, we now have this code.
From Parent.js
render() {
return <Child name={this.state.name} onChange={this.changeName} />
}
changeName(newName) {
this.setState({ name: newName });
}
So this.changeName
is being passed to the Child component on the onChange
attribute from Parent, which is why it can be called with this.props.onChange()
from within the Child component.