Files
freeCodeCamp/guide/english/certifications/front-end-libraries/react/pass-a-callback-as-props/index.md

63 lines
1.5 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Pass a Callback as Props
---
# Pass a Callback as Props
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
- Add the `GetInput` component to the render method in MyApp, then pass it a prop called `inpu`t assigned to `inputValue` from MyApp's state. Also create a prop called `handleChange` and pass the input handler `handleChange` to it.
- Add `RenderInput` to the render method in MyApp, then create a prop called `input` and pass the `inputValue` from state to it.
---
## Hints
### Hint 1
`state` is a property of `Myapp` class, so use 'this.state' to get the object value
### Hint 2
To learn more about state and props, read [State and Lifecycle](https://reactjs.org/docs/state-and-lifecycle.html) and [Components and Props](https://reactjs.org/docs/components-and-props.html).
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
class MyApp extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: ""
};
2018-10-12 15:37:13 -04:00
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
inputValue: event.target.value
});
}
render() {
return (
<div>
{
/* change code below this line */
<GetInput
input={this.state.inputValue}
handleChange={this.handleChange}
/>
2018-10-12 15:37:13 -04:00
}
{
/* change code above this line */
<RenderInput input={this.state.inputValue} />
2018-10-12 15:37:13 -04:00
}
</div>
2018-10-12 15:37:13 -04:00
);
}
}
2018-10-12 15:37:13 -04:00
```
</details>