├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── react-axios-example.png ├── src ├── App.css ├── App.js ├── http-common.js ├── index.css ├── index.js ├── logo.svg └── reportWebVitals.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Axios example with Rest API 2 | 3 | React Client with Axios to make CRUD requests to Rest API in that: 4 | 5 | - React Axios GET request: get all Tutorials, get Tutorial by Id, find Tutorial by title 6 | - React Axios POST request: create new Tutorial 7 | - React Axios PUT request: update an existing Tutorial 8 | - React Axios DELETE request: delete a Tutorial, delete all Tutorials 9 | 10 | ![react-axios-example](react-axios-example.png) 11 | 12 | For instruction, please visit: 13 | > [React Axios example - Get/Post/Put/Delete with Rest API](https://www.bezkoder.com/react-axios-example/) 14 | 15 | Related Posts: 16 | > [React Fetch example - Get/Post/Put/Delete with Rest API](https://www.bezkoder.com/react-fetch-example/) 17 | 18 | > [React + Axios: CRUD example to consume Web API](https://www.bezkoder.com/react-crud-web-api/) 19 | 20 | > [React Table example: CRUD App with react-table v7](https://www.bezkoder.com/react-table-example-hooks-crud/) 21 | 22 | Using Material UI instead of Bootstrap: 23 | 24 | > [React Material UI examples with a CRUD Application](https://www.bezkoder.com/react-material-ui-examples-crud/) 25 | 26 | More Practice: 27 | 28 | > [React Pagination example](https://www.bezkoder.com/react-pagination-material-ui/) 29 | 30 | > [React File Upload example](https://www.bezkoder.com/react-file-upload-axios/) 31 | 32 | > [React JWT Authentication & Authorization example](https://www.bezkoder.com/react-jwt-auth/) 33 | 34 | > [React + Redux: JWT Authentication & Authorization example](https://www.bezkoder.com/react-redux-jwt-auth/) 35 | 36 | Fullstack with Node Express: 37 | 38 | > [React + Node Express + MySQL](https://www.bezkoder.com/react-node-express-mysql/) 39 | 40 | > [React + Node Express + PostgreSQL](https://www.bezkoder.com/react-node-express-postgresql/) 41 | 42 | > [React + Node Express + MongoDB](https://www.bezkoder.com/react-node-express-mongodb-mern-stack/) 43 | 44 | Fullstack with Spring Boot: 45 | 46 | > [React + Spring Boot + MySQL](https://www.bezkoder.com/react-spring-boot-crud/) 47 | 48 | > [React + Spring Boot + PostgreSQL](https://www.bezkoder.com/spring-boot-react-postgresql/) 49 | 50 | > [React + Spring Boot + MongoDB](https://www.bezkoder.com/react-spring-boot-mongodb/) 51 | 52 | Fullstack with Django: 53 | 54 | > [React + Django Rest Framework](https://www.bezkoder.com/django-react-axios-rest-framework/) 55 | 56 | Serverless: 57 | 58 | > [React Firebase CRUD App with Realtime Database](https://www.bezkoder.com/react-firebase-crud/) 59 | 60 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://www.bezkoder.com/react-firestore-crud/) 61 | 62 | Integration (run back-end & front-end on same server/port) 63 | 64 | > [How to integrate React with Spring Boot](https://www.bezkoder.com/integrate-reactjs-spring-boot/) 65 | 66 | > [Integrate React with Node Express on same Server/Port](https://www.bezkoder.com/integrate-react-express-same-server-port/) 67 | 68 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-axios-example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.4", 7 | "@testing-library/react": "^11.1.0", 8 | "@testing-library/user-event": "^12.1.10", 9 | "axios": "^0.22.0", 10 | "react": "^17.0.2", 11 | "react-dom": "^17.0.2", 12 | "react-scripts": "4.0.3", 13 | "web-vitals": "^1.0.1" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "eslintConfig": { 22 | "extends": [ 23 | "react-app", 24 | "react-app/jest" 25 | ] 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-axios-example/4135f4ebac68026921b5f41d45284c75093b8a99/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | 31 | React Axios example 32 | 33 | 34 | 35 |
36 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-axios-example/4135f4ebac68026921b5f41d45284c75093b8a99/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-axios-example/4135f4ebac68026921b5f41d45284c75093b8a99/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /react-axios-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-axios-example/4135f4ebac68026921b5f41d45284c75093b8a99/react-axios-example.png -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | #app { 2 | max-width: 600px; 3 | } -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { useRef, useState } from "react"; 2 | import "./App.css"; 3 | 4 | import apiClient from "./http-common"; 5 | 6 | function App() { 7 | const get_id = useRef(null); 8 | const get_title = useRef(null); 9 | 10 | const post_title = useRef(null); 11 | const post_description = useRef(null); 12 | 13 | const put_id = useRef(null); 14 | const put_title = useRef(null); 15 | const put_description = useRef(null); 16 | const put_published = useRef(null); 17 | 18 | const delete_id = useRef(null); 19 | 20 | const [getResult, setGetResult] = useState(null); 21 | const [postResult, setPostResult] = useState(null); 22 | const [putResult, setPutResult] = useState(null); 23 | const [deleteResult, setDeleteResult] = useState(null); 24 | 25 | const fortmatResponse = (res) => { 26 | return JSON.stringify(res, null, 2); 27 | }; 28 | 29 | async function getAllData() { 30 | try { 31 | const res = await apiClient.get("/tutorials"); 32 | 33 | const result = { 34 | status: res.status + "-" + res.statusText, 35 | headers: res.headers, 36 | data: res.data, 37 | }; 38 | 39 | setGetResult(fortmatResponse(result)); 40 | } catch (err) { 41 | setGetResult(fortmatResponse(err.response?.data || err)); 42 | } 43 | } 44 | 45 | async function getDataById() { 46 | const id = get_id.current.value; 47 | 48 | if (id) { 49 | try { 50 | const res = await apiClient.get(`/tutorials/${id}`); 51 | 52 | const result = { 53 | data: res.data, 54 | status: res.status, 55 | statusText: res.statusText, 56 | headers: res.headers, 57 | }; 58 | 59 | setGetResult(fortmatResponse(result)); 60 | } catch (err) { 61 | setGetResult(fortmatResponse(err.response?.data || err)); 62 | } 63 | } 64 | } 65 | 66 | async function getDataByTitle() { 67 | const title = get_title.current.value; 68 | 69 | if (title) { 70 | try { 71 | // const res = await instance.get(`/tutorials?title=${title}`); 72 | const res = await apiClient.get("/tutorials", { 73 | params: { 74 | title: title, 75 | }, 76 | }); 77 | 78 | const result = { 79 | status: res.status + "-" + res.statusText, 80 | headers: res.headers, 81 | data: res.data, 82 | }; 83 | 84 | setGetResult(fortmatResponse(result)); 85 | } catch (err) { 86 | setGetResult(fortmatResponse(err.response?.data || err)); 87 | } 88 | } 89 | } 90 | 91 | async function postData() { 92 | const postData = { 93 | title: post_title.current.value, 94 | description: post_description.current.value, 95 | }; 96 | 97 | try { 98 | const res = await apiClient.post("/tutorials", postData, { 99 | headers: { 100 | "x-access-token": "token-value", 101 | }, 102 | }); 103 | 104 | const result = { 105 | status: res.status + "-" + res.statusText, 106 | headers: res.headers, 107 | data: res.data, 108 | }; 109 | 110 | setPostResult(fortmatResponse(result)); 111 | } catch (err) { 112 | setPostResult(fortmatResponse(err.response?.data || err)); 113 | } 114 | } 115 | 116 | async function putData() { 117 | const id = put_id.current.value; 118 | 119 | if (id) { 120 | const putData = { 121 | title: put_title.current.value, 122 | description: put_description.current.value, 123 | published: put_published.current.checked, 124 | }; 125 | 126 | try { 127 | const res = await apiClient.put(`/tutorials/${id}`, putData, { 128 | headers: { 129 | "x-access-token": "token-value", 130 | }, 131 | }); 132 | 133 | const result = { 134 | status: res.status + "-" + res.statusText, 135 | headers: res.headers, 136 | data: res.data, 137 | }; 138 | 139 | setPutResult(fortmatResponse(result)); 140 | } catch (err) { 141 | setPutResult(fortmatResponse(err.response?.data || err)); 142 | } 143 | } 144 | } 145 | 146 | async function deleteAllData() { 147 | try { 148 | const res = await apiClient.delete("/tutorials"); 149 | 150 | const result = { 151 | status: res.status + "-" + res.statusText, 152 | headers: res.headers, 153 | data: res.data, 154 | }; 155 | 156 | setDeleteResult(fortmatResponse(result)); 157 | } catch (err) { 158 | setDeleteResult(fortmatResponse(err.response?.data || err)); 159 | } 160 | } 161 | 162 | async function deleteDataById() { 163 | const id = delete_id.current.value; 164 | 165 | if (id) { 166 | try { 167 | const res = await apiClient.delete(`/tutorials/${id}`); 168 | 169 | const result = { 170 | status: res.status + "-" + res.statusText, 171 | headers: res.headers, 172 | data: res.data, 173 | }; 174 | 175 | setDeleteResult(fortmatResponse(result)); 176 | } catch (err) { 177 | setDeleteResult(fortmatResponse(err.response?.data || err)); 178 | } 179 | } 180 | } 181 | 182 | const clearGetOutput = () => { 183 | setGetResult(null); 184 | }; 185 | 186 | const clearPostOutput = () => { 187 | setPostResult(null); 188 | }; 189 | 190 | const clearPutOutput = () => { 191 | setPutResult(null); 192 | }; 193 | 194 | const clearDeleteOutput = () => { 195 | setDeleteResult(null); 196 | }; 197 | 198 | return ( 199 |
200 |

React Axios example

201 | 202 |
203 |
React Axios GET - BezKoder.com
204 |
205 |
206 | 207 | 208 | 209 |
210 | 211 |
212 | 213 | 214 |
215 | 216 |
217 | 218 | 219 |
220 | 221 | { getResult &&
{getResult}
} 222 |
223 |
224 | 225 |
226 |
React Axios POST - BezKoder.com
227 |
228 |
229 | 230 |
231 |
232 | 233 |
234 | 235 | 236 | 237 | { postResult &&
{postResult}
} 238 |
239 |
240 | 241 |
242 |
React Axios PUT - BezKoder.com
243 |
244 |
245 | 246 |
247 |
248 | 249 |
250 |
251 | 252 |
253 |
254 | 255 | 256 |
257 | 258 | 259 | 260 | { putResult &&
{putResult}
} 261 |
262 |
263 | 264 |
265 |
React Axios DELETE - BezKoder.com
266 |
267 |
268 | 269 | 270 | 271 |
272 | 273 |
274 | 275 | 276 |
277 | 278 | { deleteResult &&
{deleteResult}
} 279 |
280 |
281 | 282 |
283 | ); 284 | } 285 | 286 | export default App; 287 | -------------------------------------------------------------------------------- /src/http-common.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | export default axios.create({ 4 | baseURL: "http://localhost:8080/api", 5 | headers: { 6 | "Content-type": "application/json" 7 | } 8 | }); 9 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | --------------------------------------------------------------------------------