I am trying to do something like Hello World, but I want the user to be able to enter their first and last name, and then on the parent, it will say Hello, [FirstName] [LastName]!
Needless to say I'm pretty new to React, but I wanted to have a separate component specifically for entering the names.
App.js (parent)
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import InsertName from "./insertName";
class App extends Component {
state = {
name: ""
};
handleClick = props=> {
console.log("heyy", props.fname);
};
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React {this.state.name}</h1>
</header>
<p className="App-intro">
To get started, editLdOL <code>src/App.js</code> and save to reload.
</p>
<InsertName onClick={this.handleClick} />
</div>
);
}
}
export default App;
Child, InsertName
import React, { Component } from "react";
class InsertName extends Component {
render() {
return (
<div>
First name: <input type="text" name="fname" value={this.props.fName} />
<br />
Last name: <input type="text" name="lname" value={this.props.lName} />
<br />
<button onClick={this.props.onClick}>Click here!!</button>
</div>
);
}
}
export default InsertName;