Created
June 4, 2018 23:38
-
-
Save clc80/4229b0fd566481f8af871f94c6e695af to your computer and use it in GitHub Desktop.
Changing the state with new value
This file contains 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' | |
import './App.css'; | |
class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
formItems: ['one', 'two', 'three'], | |
newFormElement: '' | |
} | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
} | |
handleChange(e) { | |
this.setState({newFormElement: e.target.value}) | |
} | |
render() { | |
return ( | |
<div className="App"> | |
<ul> | |
{this.state.formItems.map( (element, index) => | |
<Form | |
key={index} | |
formElement={element} | |
/> | |
)} | |
</ul> | |
<form onSubmit={(e) => this.handleSubmit(e)}> | |
<input type="text" | |
value={this.state.newFormElement} | |
onChange={ (e) => this.handleChange(e)} | |
/> | |
<input type="submit" /> | |
</form> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment