├── .env ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── react-table-example-crud-react-table-v7-tutorial.png ├── src ├── App.css ├── App.js ├── components │ ├── AddTutorial.js │ ├── Tutorial.js │ └── TutorialsList.js ├── http-common.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js ├── services │ └── TutorialService.js └── setupTests.js └── yarn.lock /.env: -------------------------------------------------------------------------------- 1 | PORT=8081 -------------------------------------------------------------------------------- /.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 Table example: CRUD App with react-table v7 2 | 3 | Build a React Table example with [react-table](https://github.com/tannerlinsley/react-table) v7 by a CRUD Application to consume Web API with Axios, display and modify data with Router & Bootstrap. 4 | - Each Tutorial has id, title, description, published status. 5 | - We can create, retrieve, update, delete Tutorials. 6 | - List of Tutorials is shown in a Table using `react-table` v7 7 | - There is a Search bar for finding Tutorials by title. 8 | 9 | ![react-table-example-crud-react-table-v7-tutorial](react-table-example-crud-react-table-v7-tutorial.png) 10 | 11 | For instruction, please visit: 12 | > [React Table example: CRUD App with react-table v7](https://bezkoder.com/react-table-example-hooks-crud/) 13 | 14 | More Practice: 15 | > [React Hooks CRUD example with Axios and Web API](https://bezkoder.com/react-hooks-crud-axios-api/) 16 | 17 | > [React Pagination using Hooks example](https://bezkoder.com/react-pagination-hooks/) 18 | 19 | > [React Hooks File Upload example](https://bezkoder.com/react-hooks-file-upload/) 20 | 21 | > [React Hooks: JWT Authentication & Authorization example](https://bezkoder.com/react-hooks-jwt-auth/) 22 | 23 | > [React + Redux + Hooks: JWT Authentication & Authorization example](https://bezkoder.com/react-hooks-redux-login-registration-example/) 24 | 25 | Fullstack with Node.js Express: 26 | > [React + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/) 27 | 28 | > [React + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/) 29 | 30 | > [React + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/) 31 | 32 | Fullstack with Spring Boot: 33 | > [React + Spring Boot + MySQL](https://bezkoder.com/react-spring-boot-crud/) 34 | 35 | > [React + Spring Boot + PostgreSQL](https://bezkoder.com/spring-boot-react-postgresql/) 36 | 37 | > [React + Spring Boot + MongoDB](https://bezkoder.com/react-spring-boot-mongodb/) 38 | 39 | Fullstack with Django: 40 | > [React.js Hooks + Django Rest Framework](https://bezkoder.com/django-react-hooks/) 41 | 42 | Serverless with Firebase: 43 | > [React Firebase Hooks: CRUD App with Realtime Database example](https://bezkoder.com/react-firebase-hooks-crud/) 44 | 45 | > [React Hooks Firestore example: CRUD App](https://bezkoder.com/react-hooks-firestore/) 46 | 47 | Integration (run back-end & front-end on same server/port) 48 | > [How to integrate React.js with Spring Boot](https://bezkoder.com/integrate-reactjs-spring-boot/) 49 | 50 | > [Integrate React with Node.js Express on same Server/Port](https://bezkoder.com/integrate-react-express-same-server-port/) 51 | 52 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 53 | 54 | ### Set port 55 | .env 56 | ``` 57 | PORT=8081 58 | ``` 59 | 60 | ## Project setup 61 | 62 | In the project directory, you can run: 63 | 64 | ``` 65 | npm install 66 | # or 67 | yarn install 68 | ``` 69 | 70 | or 71 | 72 | ### Compiles and hot-reloads for development 73 | 74 | ``` 75 | npm start 76 | # or 77 | yarn start 78 | ``` 79 | 80 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser. 81 | 82 | The page will reload if you make edits. 83 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-table-crud-example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@fortawesome/fontawesome-free": "^5.15.2", 7 | "@testing-library/jest-dom": "^5.11.4", 8 | "@testing-library/react": "^11.1.0", 9 | "@testing-library/user-event": "^12.1.10", 10 | "axios": "^0.21.1", 11 | "bootstrap": "^4.6.0", 12 | "react": "^17.0.1", 13 | "react-dom": "^17.0.1", 14 | "react-router-dom": "^5.2.0", 15 | "react-scripts": "4.0.3", 16 | "react-table": "^7.6.3", 17 | "web-vitals": "^1.0.1" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": [ 27 | "react-app", 28 | "react-app/jest" 29 | ] 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-table-crud-example/04340e33bbbb8e07ef99ebd6748e232125705a60/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React Table example 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-table-crud-example/04340e33bbbb8e07ef99ebd6748e232125705a60/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-table-crud-example/04340e33bbbb8e07ef99ebd6748e232125705a60/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-table-example-crud-react-table-v7-tutorial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-table-crud-example/04340e33bbbb8e07ef99ebd6748e232125705a60/react-table-example-crud-react-table-v7-tutorial.png -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .list .action { 2 | cursor: pointer; 3 | } 4 | 5 | .submit-form { 6 | max-width: 300px; 7 | margin: auto; 8 | } 9 | 10 | .edit-form { 11 | max-width: 300px; 12 | margin: auto; 13 | } -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Switch, Route, Link } from "react-router-dom"; 3 | import "bootstrap/dist/css/bootstrap.min.css"; 4 | import "@fortawesome/fontawesome-free/css/all.css"; 5 | import "@fortawesome/fontawesome-free/js/all.js"; 6 | import "./App.css"; 7 | 8 | import AddTutorial from "./components/AddTutorial"; 9 | import Tutorial from "./components/Tutorial"; 10 | import TutorialsList from "./components/TutorialsList"; 11 | 12 | function App() { 13 | return ( 14 |
15 | 32 | 33 |
34 | 35 | 36 | 37 | 38 | 39 |
40 |
41 | ); 42 | } 43 | 44 | export default App; 45 | -------------------------------------------------------------------------------- /src/components/AddTutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import TutorialDataService from "../services/TutorialService"; 3 | 4 | const AddTutorial = () => { 5 | const initialTutorialState = { 6 | id: null, 7 | title: "", 8 | description: "", 9 | published: false 10 | }; 11 | const [tutorial, setTutorial] = useState(initialTutorialState); 12 | const [submitted, setSubmitted] = useState(false); 13 | 14 | const handleInputChange = event => { 15 | const { name, value } = event.target; 16 | setTutorial({ ...tutorial, [name]: value }); 17 | }; 18 | 19 | const saveTutorial = () => { 20 | var data = { 21 | title: tutorial.title, 22 | description: tutorial.description 23 | }; 24 | 25 | TutorialDataService.create(data) 26 | .then(response => { 27 | setTutorial({ 28 | id: response.data.id, 29 | title: response.data.title, 30 | description: response.data.description, 31 | published: response.data.published 32 | }); 33 | setSubmitted(true); 34 | console.log(response.data); 35 | }) 36 | .catch(e => { 37 | console.log(e); 38 | }); 39 | }; 40 | 41 | const newTutorial = () => { 42 | setTutorial(initialTutorialState); 43 | setSubmitted(false); 44 | }; 45 | 46 | return ( 47 |
48 | {submitted ? ( 49 |
50 |

You submitted successfully!

51 | 54 |
55 | ) : ( 56 |
57 |
58 | 59 | 68 |
69 | 70 |
71 | 72 | 81 |
82 | 83 | 86 |
87 | )} 88 |
89 | ); 90 | }; 91 | 92 | export default AddTutorial; 93 | -------------------------------------------------------------------------------- /src/components/Tutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import TutorialDataService from "../services/TutorialService"; 3 | 4 | const Tutorial = props => { 5 | const initialTutorialState = { 6 | id: null, 7 | title: "", 8 | description: "", 9 | published: false 10 | }; 11 | const [currentTutorial, setCurrentTutorial] = useState(initialTutorialState); 12 | const [message, setMessage] = useState(""); 13 | 14 | const getTutorial = id => { 15 | TutorialDataService.get(id) 16 | .then(response => { 17 | setCurrentTutorial(response.data); 18 | console.log(response.data); 19 | }) 20 | .catch(e => { 21 | console.log(e); 22 | }); 23 | }; 24 | 25 | useEffect(() => { 26 | getTutorial(props.match.params.id); 27 | }, [props.match.params.id]); 28 | 29 | const handleInputChange = event => { 30 | const { name, value } = event.target; 31 | setCurrentTutorial({ ...currentTutorial, [name]: value }); 32 | }; 33 | 34 | const updatePublished = status => { 35 | var data = { 36 | id: currentTutorial.id, 37 | title: currentTutorial.title, 38 | description: currentTutorial.description, 39 | published: status 40 | }; 41 | 42 | TutorialDataService.update(currentTutorial.id, data) 43 | .then(response => { 44 | setCurrentTutorial({ ...currentTutorial, published: status }); 45 | console.log(response.data); 46 | setMessage("The status was updated successfully!"); 47 | }) 48 | .catch(e => { 49 | console.log(e); 50 | }); 51 | }; 52 | 53 | const updateTutorial = () => { 54 | TutorialDataService.update(currentTutorial.id, currentTutorial) 55 | .then(response => { 56 | console.log(response.data); 57 | setMessage("The tutorial was updated successfully!"); 58 | }) 59 | .catch(e => { 60 | console.log(e); 61 | }); 62 | }; 63 | 64 | const deleteTutorial = () => { 65 | TutorialDataService.remove(currentTutorial.id) 66 | .then(response => { 67 | console.log(response.data); 68 | props.history.push("/tutorials"); 69 | }) 70 | .catch(e => { 71 | console.log(e); 72 | }); 73 | }; 74 | 75 | return ( 76 |
77 | {currentTutorial ? ( 78 |
79 |

Tutorial

80 |
81 |
82 | 83 | 91 |
92 |
93 | 94 | 102 |
103 | 104 |
105 | 108 | {currentTutorial.published ? "Published" : "Pending"} 109 |
110 |
111 | 112 | {currentTutorial.published ? ( 113 | 119 | ) : ( 120 | 126 | )} 127 | 128 | 131 | 132 | 139 |

{message}

140 |
141 | ) : ( 142 |
143 |
144 |

Please click on a Tutorial...

145 |
146 | )} 147 |
148 | ); 149 | }; 150 | 151 | export default Tutorial; 152 | -------------------------------------------------------------------------------- /src/components/TutorialsList.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, useMemo, useRef } from "react"; 2 | import TutorialDataService from "../services/TutorialService"; 3 | import { useTable } from "react-table"; 4 | 5 | const TutorialsList = (props) => { 6 | const [tutorials, setTutorials] = useState([]); 7 | const [searchTitle, setSearchTitle] = useState(""); 8 | const tutorialsRef = useRef(); 9 | 10 | tutorialsRef.current = tutorials; 11 | 12 | useEffect(() => { 13 | retrieveTutorials(); 14 | }, []); 15 | 16 | const onChangeSearchTitle = (e) => { 17 | const searchTitle = e.target.value; 18 | setSearchTitle(searchTitle); 19 | }; 20 | 21 | const retrieveTutorials = () => { 22 | TutorialDataService.getAll() 23 | .then((response) => { 24 | setTutorials(response.data); 25 | }) 26 | .catch((e) => { 27 | console.log(e); 28 | }); 29 | }; 30 | 31 | const refreshList = () => { 32 | retrieveTutorials(); 33 | }; 34 | 35 | const removeAllTutorials = () => { 36 | TutorialDataService.removeAll() 37 | .then((response) => { 38 | console.log(response.data); 39 | refreshList(); 40 | }) 41 | .catch((e) => { 42 | console.log(e); 43 | }); 44 | }; 45 | 46 | const findByTitle = () => { 47 | TutorialDataService.findByTitle(searchTitle) 48 | .then((response) => { 49 | setTutorials(response.data); 50 | }) 51 | .catch((e) => { 52 | console.log(e); 53 | }); 54 | }; 55 | 56 | const openTutorial = (rowIndex) => { 57 | const id = tutorialsRef.current[rowIndex].id; 58 | 59 | props.history.push("/tutorials/" + id); 60 | }; 61 | 62 | const deleteTutorial = (rowIndex) => { 63 | const id = tutorialsRef.current[rowIndex].id; 64 | 65 | TutorialDataService.remove(id) 66 | .then((response) => { 67 | props.history.push("/tutorials"); 68 | 69 | let newTutorials = [...tutorialsRef.current]; 70 | newTutorials.splice(rowIndex, 1); 71 | 72 | setTutorials(newTutorials); 73 | }) 74 | .catch((e) => { 75 | console.log(e); 76 | }); 77 | }; 78 | 79 | const columns = useMemo( 80 | () => [ 81 | { 82 | Header: "Title", 83 | accessor: "title", 84 | }, 85 | { 86 | Header: "Description", 87 | accessor: "description", 88 | }, 89 | { 90 | Header: "Status", 91 | accessor: "published", 92 | Cell: (props) => { 93 | return props.value ? "Published" : "Pending"; 94 | }, 95 | }, 96 | { 97 | Header: "Actions", 98 | accessor: "actions", 99 | Cell: (props) => { 100 | const rowIdx = props.row.id; 101 | return ( 102 |
103 | openTutorial(rowIdx)}> 104 | 105 | 106 | 107 | deleteTutorial(rowIdx)}> 108 | 109 | 110 |
111 | ); 112 | }, 113 | }, 114 | ], 115 | [] 116 | ); 117 | 118 | const { 119 | getTableProps, 120 | getTableBodyProps, 121 | headerGroups, 122 | rows, 123 | prepareRow, 124 | } = useTable({ 125 | columns, 126 | data: tutorials, 127 | }); 128 | 129 | return ( 130 |
131 |
132 |
133 | 140 |
141 | 148 |
149 |
150 |
151 |
152 | 156 | 157 | {headerGroups.map((headerGroup) => ( 158 | 159 | {headerGroup.headers.map((column) => ( 160 | 163 | ))} 164 | 165 | ))} 166 | 167 | 168 | {rows.map((row, i) => { 169 | prepareRow(row); 170 | return ( 171 | 172 | {row.cells.map((cell) => { 173 | return ( 174 | 175 | ); 176 | })} 177 | 178 | ); 179 | })} 180 | 181 |
161 | {column.render("Header")} 162 |
{cell.render("Cell")}
182 |
183 | 184 |
185 | 188 |
189 |
190 | ); 191 | }; 192 | 193 | export default TutorialsList; 194 | -------------------------------------------------------------------------------- /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 | }); -------------------------------------------------------------------------------- /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 { BrowserRouter } from "react-router-dom"; 4 | 5 | import App from "./App"; 6 | import reportWebVitals from './reportWebVitals'; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | , 12 | document.getElementById("root") 13 | ); 14 | 15 | reportWebVitals(); 16 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/services/TutorialService.js: -------------------------------------------------------------------------------- 1 | import http from "../http-common"; 2 | 3 | const getAll = () => { 4 | return http.get("/tutorials"); 5 | }; 6 | 7 | const get = (id) => { 8 | return http.get(`/tutorials/${id}`); 9 | }; 10 | 11 | const create = (data) => { 12 | return http.post("/tutorials", data); 13 | }; 14 | 15 | const update = (id, data) => { 16 | return http.put(`/tutorials/${id}`, data); 17 | }; 18 | 19 | const remove = (id) => { 20 | return http.delete(`/tutorials/${id}`); 21 | }; 22 | 23 | const removeAll = () => { 24 | return http.delete(`/tutorials`); 25 | }; 26 | 27 | const findByTitle = (title) => { 28 | return http.get(`/tutorials?title=${title}`); 29 | }; 30 | 31 | const TutorialService = { 32 | getAll, 33 | get, 34 | create, 35 | update, 36 | remove, 37 | removeAll, 38 | findByTitle, 39 | }; 40 | 41 | export default TutorialService; 42 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | --------------------------------------------------------------------------------