Last active
April 23, 2019 23:18
-
-
Save connor11528/0c72a71820456df690d48b04f76ed4a8 to your computer and use it in GitHub Desktop.
React.js component for sending a POST request
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 axios from "axios" | |
| export default class AddSalesperson extends React.Component { | |
| state = { | |
| name: '', | |
| } | |
| submit = event => { | |
| event.preventDefault(); | |
| axios.post('FUNCTION_PATH_HERE', { name: this.state.name }) | |
| .then(res => { | |
| console.log(res); | |
| console.log(res.data); | |
| }) | |
| .catch(error => { | |
| console.log(error); | |
| }); | |
| this.setState({ | |
| name: '' | |
| }); | |
| } | |
| handleInputChange = event => { | |
| const target = event.target | |
| const value = target.value | |
| const name = target.name | |
| this.setState({ | |
| [name]: value, | |
| }); | |
| } | |
| render(){ | |
| return ( | |
| <form onSubmit={this.submit}> | |
| <input | |
| placeholder="Salesperson's name?" | |
| name='name' | |
| value={this.state.name} | |
| onChange={this.handleInputChange} | |
| /> | |
| <input type="submit" value="Add" /> | |
| </form> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment