react axios post method
import React from 'react';
import axios from 'axios';
export default class PersonList extends React.Component {
state = {
name: '',
}
handleChange = event => {
this.setState({ name: event.target.value });
}
handleSubmit = event => {
event.preventDefault();
const user = {
name: this.state.name
};
axios.post(`https://jsonplaceholder.typicode.com/users`, { user })
.then(res => {
console.log(res);
console.log(res.data);
})
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
Person Name:
<input type="text" name="name" onChange={this.handleChange} />
</label>
<button type="submit">Add</button>
</form>
</div>
)
}
}
import React from 'react';
import axios from 'axios';
export default class PersonList extends React.Component {
state = {
persons: []
}
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
}
render() {
return (
<ul>
{ this.state.persons.map(person => <li>{person.name}</li>)}
</ul>
)
}
}
Also in JavaScript:
- compare dates in js
- float to euro curency
- how to query chain an id in an id javascript
- compare arrays javascript
- post express node js input
- javascript on submit
- moving letters animation javascript
- javascript find the min in array of numbers
- tranary operator in js
- removing duplicates from array javascript
- javascript count down with wile loop
- how to get date time day year in javscript
- how to flatten array with reduce in javascript
- node loop files and push to array to display on screen with nunjucks
- telerik asp.net ajax error creating control
- js xmlhttprequest add header
- javascript get class name
- Javascript get month name
- ngif is string angular
- how to change grid size with conditional expression in react
- jquery works until modal is shown
- gatsby underline link if page is active
- brwoser prompt before reload
- node ja sap concur