Skip to content

Instantly share code, notes, and snippets.

@clc80
Created June 4, 2018 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clc80/4229b0fd566481f8af871f94c6e695af to your computer and use it in GitHub Desktop.
Save clc80/4229b0fd566481f8af871f94c6e695af to your computer and use it in GitHub Desktop.
Changing the state with new value
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