Skip to content

Commit fd64dca

Browse files
Axios
1 parent 630820e commit fd64dca

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

react.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,3 +2367,49 @@ function mapDispatchToProps(dispatch) {
23672367
}
23682368
export default connect(mapStateToProps, mapDispatchToProps)(App);
23692369

2370+
2371+
2372+
// AXIOS
2373+
// get rid of JSON methods as it is handled by axios
2374+
// npm install axios
2375+
2376+
const url = 'http://localhost:3333/some';
2377+
// GET
2378+
// Fetch way
2379+
async function getSomeFetch() {
2380+
await fetch(url, {method: 'GET', /*params*/})
2381+
}
2382+
// Axios way
2383+
async function getSomeAxios() {
2384+
await axios.get(url, {/*params*/})
2385+
}
2386+
2387+
// POST
2388+
// Fetch way
2389+
async function postSomeFetch(id) {
2390+
await fetch(url, {method: 'POST', body: JSON.stringify({id: id}), /*params*/})
2391+
}
2392+
// Axios way
2393+
async function postSomeAxios(id) {
2394+
await axios.post(url, { id: id }, {/*params*/})
2395+
}
2396+
2397+
// DELETE
2398+
// Fetch way
2399+
async function deleteSomeFetch(id) {
2400+
await fetch(url, {method: 'DELETE', body: JSON.stringify({id: id}), /*params*/})
2401+
}
2402+
// Axios way
2403+
async function deleteSomeAxios(id) {
2404+
await axios.delete(url, {data: { id: id }, /*params*/})
2405+
}
2406+
2407+
// PATCH
2408+
// Fetch way
2409+
async function patchSomeFetch(id, quantity) {
2410+
await fetch(url, {method: 'PATCH', body: JSON.stringify({id: id, quantity: quantity}), /*params*/})
2411+
}
2412+
// Axios way
2413+
async function patchSomeAxios(id, quantity) {
2414+
await axios.patch(url, { id: id, quantity: quantity }, {/*params*/})
2415+
}

0 commit comments

Comments
 (0)