Created
August 19, 2021 23:21
-
-
Save NickFoden/f952c0d742757ac0725fb07b99a037c9 to your computer and use it in GitHub Desktop.
Inputs Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import Form from "./components/Form"; | |
class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
finalInputState: {} | |
}; | |
} | |
handleSubmit = (stuff) => { | |
console.dir(stuff); | |
}; | |
render() { | |
return ( | |
<div className="App"> | |
<Form handleSubmit={this.handleSubmit}/> | |
</div> | |
// <OtherRoute data={this.state.finalInputState} /> | |
); | |
} | |
} | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
class Form extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
input:{ | |
firstName: "", | |
age: "", | |
} | |
}; | |
} | |
handleInputChange = (e) => { | |
const { name, value } = e.target; | |
this.setState(state => ({ | |
...state, | |
input:{ | |
...state.input, | |
[name]: value, | |
} | |
})); | |
}; | |
handleSubmit = (e) => { | |
e.preventDefault(); | |
this.props.handleSubmit(this.state) | |
}; | |
render() { | |
return ( | |
<div className="App"> | |
<form> | |
<label> | |
First Name | |
<input onChange={this.handleInputChange} name="firstName" /> | |
</label> | |
<label> | |
Age | |
<input onChange={this.handleInputChange} name="age" /> | |
</label> | |
{/* Button type influence actions */} | |
<button type="button" onClick={this.handleSubmit}> | |
Submit | |
</button> | |
</form> | |
</div> | |
); | |
} | |
} | |
export default Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment