Last active
January 14, 2016 04:46
-
-
Save cacheflow/8429c7cabd610e26926d to your computer and use it in GitHub Desktop.
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 from 'react'; | |
| import ReactDOM from 'react-dom'; | |
| export default class Form extends React.Component{ | |
| constructor(){ | |
| super(); | |
| this.state = { | |
| names: [] | |
| } | |
| } | |
| handleSubmit(){ | |
| let firstName = this.refs.firstName.value; | |
| let lastName = this.refs.lastName.value; | |
| let person = {firstName: firstName, lastName: lastName}; | |
| this.refs.firstName.value = ""; | |
| this.refs.lastName.value = ""; | |
| let oldNameState = this.state.names; | |
| let newNameState = oldNameState.concat(person) | |
| this.setState({ | |
| names: newNameState | |
| }); | |
| console.log("your new state is", this.state.names); | |
| } | |
| render(){ | |
| let names = this.state.names.map((person, index) => { | |
| return ( | |
| <div id="person" key={person + index}> | |
| <div>Hello {person.firstName}</div> | |
| <div>{person.lastName}</div> | |
| </div> | |
| ) | |
| }); | |
| return ( | |
| <div> | |
| First Name <input type="text" ref="firstName" /> | |
| Last Name<input type="text" ref="lastName" /> | |
| <button onClick={this.handleSubmit.bind(this)}> Submit</button> | |
| {names} | |
| </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment