├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json └── src ├── App.js ├── forms ├── AddUserForm.js └── EditUserForm.js ├── index.css ├── index.js └── tables └── UserTable.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Hooks Tutorial 2 | 3 | In this tutorial, we'll make a simple CRUD app that can add, update, or delete users. 4 | 5 | ### [View the demo](https://taniarascia.github.io/react-hooks/) | [Read the tutorial](https://www.taniarascia.com/crud-app-in-react-with-hooks/) 6 | 7 | ## Author 8 | 9 | - [Tania Rascia](https://www.taniarascia.com) 10 | 11 | ## License 12 | 13 | This project is open source and available under the [MIT License](LICENSE). 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-hooks", 3 | "version": "0.1.1", 4 | "private": true, 5 | "dependencies": { 6 | "gh-pages": "^2.1.1", 7 | "react": "^16.9.0", 8 | "react-dom": "^16.9.0", 9 | "react-scripts": "^3.1.1" 10 | }, 11 | "scripts": { 12 | "start": "react-scripts start", 13 | "build": "react-scripts build", 14 | "test": "react-scripts test", 15 | "eject": "react-scripts eject", 16 | "predeploy": "npm run build", 17 | "deploy": "gh-pages -d build" 18 | }, 19 | "eslintConfig": { 20 | "extends": "react-app" 21 | }, 22 | "browserslist": [ 23 | ">0.2%", 24 | "not dead", 25 | "not ie <= 11", 26 | "not op_mini all" 27 | ], 28 | "homepage": "https://taniarascia.github.io/react-hooks" 29 | } 30 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taniarascia/react-hooks/d2ebfa066db3f829ed4f3b9f38d5f72f17600b82/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 23 | React CRUD App with Hooks 24 | 25 | 26 | 29 |
30 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /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 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { useState, Fragment } from 'react' 2 | import AddUserForm from './forms/AddUserForm' 3 | import EditUserForm from './forms/EditUserForm' 4 | import UserTable from './tables/UserTable' 5 | 6 | const App = () => { 7 | // Data 8 | const usersData = [ 9 | { id: 1, name: 'Tania', username: 'floppydiskette' }, 10 | { id: 2, name: 'Craig', username: 'siliconeidolon' }, 11 | { id: 3, name: 'Ben', username: 'benisphere' }, 12 | ] 13 | 14 | const initialFormState = { id: null, name: '', username: '' } 15 | 16 | // Setting state 17 | const [ users, setUsers ] = useState(usersData) 18 | const [ currentUser, setCurrentUser ] = useState(initialFormState) 19 | const [ editing, setEditing ] = useState(false) 20 | 21 | // CRUD operations 22 | const addUser = user => { 23 | user.id = users.length + 1 24 | setUsers([ ...users, user ]) 25 | } 26 | 27 | const deleteUser = id => { 28 | setEditing(false) 29 | 30 | setUsers(users.filter(user => user.id !== id)) 31 | } 32 | 33 | const updateUser = (id, updatedUser) => { 34 | setEditing(false) 35 | 36 | setUsers(users.map(user => (user.id === id ? updatedUser : user))) 37 | } 38 | 39 | const editRow = user => { 40 | setEditing(true) 41 | 42 | setCurrentUser({ id: user.id, name: user.name, username: user.username }) 43 | } 44 | 45 | return ( 46 |
47 |

CRUD App with Hooks

48 |
49 |
50 | {editing ? ( 51 | 52 |

Edit user

53 | 59 |
60 | ) : ( 61 | 62 |

Add user

63 | 64 |
65 | )} 66 |
67 |
68 |

View users

69 | 70 |
71 |
72 |
73 | ) 74 | } 75 | 76 | export default App 77 | -------------------------------------------------------------------------------- /src/forms/AddUserForm.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react' 2 | 3 | const AddUserForm = props => { 4 | const initialFormState = { id: null, name: '', username: '' } 5 | const [ user, setUser ] = useState(initialFormState) 6 | 7 | const handleInputChange = event => { 8 | const { name, value } = event.target 9 | 10 | setUser({ ...user, [name]: value }) 11 | } 12 | 13 | return ( 14 |
{ 16 | event.preventDefault() 17 | if (!user.name || !user.username) return 18 | 19 | props.addUser(user) 20 | setUser(initialFormState) 21 | }} 22 | > 23 | 24 | 25 | 26 | 27 | 28 |
29 | ) 30 | } 31 | 32 | export default AddUserForm 33 | -------------------------------------------------------------------------------- /src/forms/EditUserForm.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react' 2 | 3 | const EditUserForm = props => { 4 | const [ user, setUser ] = useState(props.currentUser) 5 | 6 | useEffect( 7 | () => { 8 | setUser(props.currentUser) 9 | }, 10 | [ props ] 11 | ) 12 | // You can tell React to skip applying an effect if certain values haven’t changed between re-renders. [ props ] 13 | 14 | const handleInputChange = event => { 15 | const { name, value } = event.target 16 | 17 | setUser({ ...user, [name]: value }) 18 | } 19 | 20 | return ( 21 |
{ 23 | event.preventDefault() 24 | 25 | props.updateUser(user.id, user) 26 | }} 27 | > 28 | 29 | 30 | 31 | 32 | 33 | 36 |
37 | ) 38 | } 39 | 40 | export default EditUserForm 41 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | button, 2 | .button { 3 | margin-bottom: 0 !important; 4 | margin-right: .5rem; 5 | } 6 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | import './index.css' 4 | import App from './App' 5 | 6 | ReactDOM.render(, document.getElementById('root')) 7 | -------------------------------------------------------------------------------- /src/tables/UserTable.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | const UserTable = props => ( 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | {props.users.length > 0 ? ( 14 | props.users.map(user => ( 15 | 16 | 17 | 18 | 34 | 35 | )) 36 | ) : ( 37 | 38 | 39 | 40 | )} 41 | 42 |
NameUsernameActions
{user.name}{user.username} 19 | 27 | 33 |
No users
43 | ) 44 | 45 | export default UserTable 46 | --------------------------------------------------------------------------------