Created
June 4, 2018 23:55
-
-
Save clc80/6e83180abb46723bb0df19cf29a02549 to your computer and use it in GitHub Desktop.
Finished app
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: [], | |
newFormElement: '' | |
} | |
} | |
handleChange(e) { | |
this.setState({newFormElement: e.target.value}) | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
if(!this.state.newFormElement) {return} | |
this.setState({formItems: [...this.state.formItems, this.state.newFormElement], newFormElement: '' }); | |
} | |
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; |
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'; | |
class Form extends Component { | |
render() { | |
return( | |
<div className="Form"> | |
<li> {this.props.formElement} </li> | |
</div> | |
); | |
} | |
} | |
export default Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment