Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created December 17, 2017 04:00
Show Gist options
  • Save Nicknyr/f644e25dd5f84aaa3d6a2bfe1bf01d1e to your computer and use it in GitHub Desktop.
Save Nicknyr/f644e25dd5f84aaa3d6a2bfe1bf01d1e to your computer and use it in GitHub Desktop.
Updating state in React with a form input box example
import React, { Component } from 'react';
import './App.css';
export default class ToDoList extends Component {
constructor(props) {
super(props);
this.state = {
userInput: '',
list: []
}
}
changeUserInput(input) {
this.setState({
userInput: input
});
}
render() {
return (
<div className="to-do-list-main">
<input
onChange={ (e) => this.changeUserInput(e.target.value)}
value={this.state.userInput}
type="text"
/>
<button>Press Me</button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment