Tell us what’s happening:
Hi, I’m totally new to React and learning it with freecodecamp’s tutorials.
In the curriculum, I understand how Child component passing the data from inputted value but I fully don’t get why Parent component calls child component using “input” instead of “inputValue”
Since the state key kept in Perent component is “inputValue”, I thought the code where calling child component should be as below:
The code I thought it should be the answer
class MyApp extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
inputValue: event.target.value
});
}
render() {
return (
<div>
<GetInput
inputValue={this.state.inputValue}
handleChange={this.handleChange}/>
<RenderInput
inputValue={this.state.inputValue}/>
</div>
);
}
};
class GetInput extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h3>Get Input:</h3>
<input
value={this.props.inputValue}
onChange={this.props.handleChange}/>
</div>
);
}
};
class RenderInput extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h3>Input Render:</h3>
<p>{this.props.inputValue}</p>
</div>
);
}
};
I don’t understand where “input” in the below model answer comes from…
Model Answer by freecodecamp
class MyApp extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
inputValue: event.target.value
});
}
render() {
return (
<div>
<GetInput
input={this.state.inputValue}
handleChange={this.handleChange}/>
<RenderInput
input={this.state.inputValue}/>
</div>
);
}
};
class GetInput extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h3>Get Input:</h3>
<input
value={this.props.input}
onChange={this.props.handleChange}/>
</div>
);
}
};
class RenderInput extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h3>Input Render:</h3>
<p>{this.props.input}</p>
</div>
);
}
};
I appreciate if someone could help me understand.
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15
.
Challenge: Pass State as Props to Child Components
Link to the challenge:
https://www.freecodecamp.org/learn/front-end-libraries/react/pass-state-as-props-to-child-components