xxxxxxxxxx
$ npm install react-axios
/ AXIOS WITH REACT APP IN BOTH CLASS/FUNCTION COMPONENTS:
// Class Component:
// =========*[GET]:
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
}
// =========*[POST]:
handelSubmit() {
const user = { name: "hello world", age: 20 };
axios.post(`https://jsonplaceholder.typicode.com/users`, { user })
.then(res => {
console.log(res.data);
})
}
//-----------------------------------------------------------------------------------
// Function Component:
// =========*[GET]:
useEffect(()=>{
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
},[]);
//=========*[POST]:
const handelSubmit = ()=>{
const user = { name: "hello world", age: 20 };
axios.post(`https://jsonplaceholder.typicode.com/users`, { user })
.then(res => {
console.log(res.data);
})
}
xxxxxxxxxx
// AXIOS WITH REACT APP IN BOTH CLASS/FUNCTION COMPONENTS:
// Class Component:
// =========*[GET]:
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
}
// =========*[POST]:
handelSubmit() {
const user = { name: "hello world", age: 20 };
axios.post(`https://jsonplaceholder.typicode.com/users`, { user })
.then(res => {
console.log(res.data);
})
}
//-----------------------------------------------------------------------------------
// Function Component:
// =========*[GET]:
useEffect(()=>{
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
},[]);
//=========*[POST]:
const handelSubmit = ()=>{
const user = { name: "hello world", age: 20 };
axios.post(`https://jsonplaceholder.typicode.com/users`, { user })
.then(res => {
console.log(res.data);
})
}
xxxxxxxxxx
const axios = require('axios');
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
axios({
method: 'get',
url: 'http://bit.ly/2mTM3nY',
responseType: 'stream'
})
.then(function (response) {
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});
axios(url[, config])
// Send a GET request (default method)
axios('/user/12345');