├── .firebase └── hosting.YnVpbGQ.cache ├── .firebaserc ├── .gitignore ├── .netlify └── state.json ├── README.md ├── firebase.json ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── App.test.js ├── Button.js ├── CongratulationsMessage.js ├── CounterButton.js ├── DisplayIf.js ├── Greeting.js ├── NavBar.js ├── PeopleList.js ├── PeopleListItem.css ├── PeopleListItem.js ├── ThemeContext.js ├── UserDataLoader.js ├── class-based ├── CongratulationsMessageCB.js ├── CounterButtonCB.js └── CounterButtonPageCB.js ├── index.css ├── index.js ├── logo.svg ├── pages ├── ControlledFormPage.js ├── CounterButtonPage.js ├── HomePage.js ├── NotFoundPage.js ├── PeopleListPage.js ├── ProtectedPage.js ├── UncontrolledFormPage.js ├── UserProfilePage.js └── index.js ├── serviceWorker.js ├── setupTests.js ├── usePersistentState.js └── useUserData.js /.firebase/hosting.YnVpbGQ.cache: -------------------------------------------------------------------------------- 1 | asset-manifest.json,1592250624046,7fac0bdb52ef76ecf0b20f439a826eee6b18a08e6a087f4d07359252cf459237 2 | index.html,1592250624046,cb3dd0808b3e4b52d59d0b215ea947d9cba6d7fba4844c751e926ea1e23faee7 3 | favicon.ico,1592250618235,a08fa4488c3ecef62d9effd03b3a989929bdcbecf5e905941f9034a15bd3dba3 4 | logo192.png,1592250618236,caff018b7f1e8fd481eb1c50d75b0ef236bcd5078b1d15c8bb348453fee30293 5 | manifest.json,1592250618238,341d52628782f8ac9290bbfc43298afccb47b7cbfcee146ae30cf0f46bc30900 6 | precache-manifest.2bb7af62df3ee23d33416f97cf29d2fe.js,1592250624046,b50cee5c58a47a630486fdfd72a9b25ce8239a87d9335e34efaa7f8ce248c4ac 7 | robots.txt,1592250618238,391d14b3c2f8c9143a27a28c7399585142228d4d1bdbe2c87ac946de411fa9a2 8 | logo512.png,1592250618237,191fc21360b4ccfb1cda11a1efb97f489ed22672ca83f4064316802bbfdd750e 9 | static/css/main.73ecec61.chunk.css,1592250624047,88db6453599bd5e5f5374efc03eaab36eaf11a7cb03f00b5df7c17849f11376a 10 | service-worker.js,1592250624046,2d3d8c6054e332dfe52c0d2558f413225b82f64a4d70992db5ae797516cb9c7d 11 | static/css/main.73ecec61.chunk.css.map,1592250624048,396974288407352ee45df45bdc075d4dbd35e8658195fd780c96e4e9e99f371b 12 | static/js/2.023ae1d1.chunk.js.LICENSE.txt,1592250624048,939252c822144d73596651aefa66be9e6a80d502374ef41a485d24f58127feac 13 | static/js/main.7f27562c.chunk.js,1592250624048,2e66a1aeff29cfaf0e2b36926dc8ba0bbec609a141d3e19bf2f921c1871aa6fc 14 | static/js/runtime-main.8eb335e6.js.map,1592250624048,76a0e3f52f3535814c3cb322bca594f32dc9a2e778659b6f7cdae4739bd77bd3 15 | static/js/runtime-main.8eb335e6.js,1592250624049,5882afb1343152af50d7959e8a24c3a35d6c9ed2a9c4a4895580ac01ea0d71d2 16 | static/js/main.7f27562c.chunk.js.map,1592250624048,906c65ae00874953c62031cb7bacecc97761d46049c155a566e0fd2e2de6c424 17 | static/js/2.023ae1d1.chunk.js,1592250624048,a4848cbb8f591ea0d5b68106da49706b4e87d0164abdaa13a152ddde0c232b8f 18 | static/js/2.023ae1d1.chunk.js.map,1592250624048,55f470b5c315254d26bb176966cc9650d8c9ba5d70c8d62cf4c6b299aeb2971a 19 | -------------------------------------------------------------------------------- /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "deploy-react-e53b6" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.netlify/state.json: -------------------------------------------------------------------------------- 1 | { 2 | "siteId": "217edba0-8baa-4756-b75e-1334f1ea1f1d" 3 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "public": "build", 4 | "ignore": [ 5 | "firebase.json", 6 | "**/.*", 7 | "**/node_modules/**" 8 | ], 9 | "rewrites": [ 10 | { 11 | "source": "**", 12 | "destination": "/index.html" 13 | } 14 | ] 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-react-app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "homepage": "http://shaunwa.github.io/my-react-app", 6 | "dependencies": { 7 | "@testing-library/jest-dom": "4.2.4", 8 | "@testing-library/react": "9.5.0", 9 | "@testing-library/user-event": "7.2.1", 10 | "query-string": "6.12.1", 11 | "react": "^16.13.1", 12 | "react-dom": "^16.13.1", 13 | "react-router-dom": "5.1.2", 14 | "react-scripts": "3.4.1", 15 | "styled-components": "5.1.0" 16 | }, 17 | "scripts": { 18 | "start": "react-scripts start", 19 | "build": "react-scripts build", 20 | "test": "react-scripts test", 21 | "eject": "react-scripts eject", 22 | "deploy:netlify": "npm run build && netlify deploy --dir ./build", 23 | "deploy:s3": "npm run build && aws s3 sync build/ s3://shauns-react-app", 24 | "deploy:firebase": "npm run build && firebase deploy", 25 | "deploy:github": "npm run build && gh-pages -d build" 26 | }, 27 | "eslintConfig": { 28 | "extends": "react-app" 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | }, 42 | "devDependencies": { 43 | "gh-pages": "3.0.0", 44 | "netlify-cli": "2.53.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaunwa/my-react-app/622ae9a76252db7c14bd140e7088e6ba2472bb8d/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaunwa/my-react-app/622ae9a76252db7c14bd140e7088e6ba2472bb8d/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaunwa/my-react-app/622ae9a76252db7c14bd140e7088e6ba2472bb8d/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 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | 40 | .people-list-heading { 41 | color: red; 42 | } 43 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; 3 | import { ThemeContext } from './ThemeContext'; 4 | import { NavBar } from './NavBar'; 5 | import { 6 | ControlledFormPage, 7 | HomePage, 8 | CounterButtonPage, 9 | NotFoundPage, 10 | PeopleListPage, 11 | ProtectedPage, 12 | UncontrolledFormPage, 13 | UserProfilePage, 14 | } from './pages'; 15 | import { UserDataLoader } from './UserDataLoader'; 16 | import { CounterButtonPageCB } from './class-based/CounterButtonPageCB'; 17 | import './App.css'; 18 | 19 | const appLinks = [ 20 | { url: '/', label: 'Home' }, 21 | { url: '/counter', label: 'Counter' }, 22 | { url: '/people-list', label: 'People List' }, 23 | { url: '/forms', label: 'Forms' }, 24 | { url: '/user', label: 'User Profile' }, 25 | ]; 26 | 27 | const formLinks = [ 28 | { url: '/forms/controlled', label: 'Controlled' }, 29 | { url: '/forms/uncontrolled', label: 'Uncontrolled' }, 30 | ]; 31 | 32 | function App() { 33 | return ( 34 | 35 |
36 | 37 | 38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 |
74 |
75 |
76 |
77 | ); 78 | } 79 | 80 | export default App; 81 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/Button.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import styled from 'styled-components'; 3 | 4 | const StyledButton = styled.button` 5 | background-color: ${props => props.buttonColor || 'transparent'}; 6 | border: 4px solid white; 7 | border-radius: 4px; 8 | color: white; 9 | cursor: pointer; 10 | font-size: 16px; 11 | font-weight: bold; 12 | margin: 8px; 13 | outline: none; 14 | padding: 16px; 15 | `; 16 | 17 | export const Button = ({ children, buttonColor, ...props }) => 18 | {children}; 19 | 20 | export const DangerButton = ({ children, ...props }) => 21 | ; 22 | 23 | export const SuccessButton = ({ children, ...props }) => 24 | ; 25 | 26 | // export class Button extends React.Component { 27 | // constructor(props) { 28 | // super(props); 29 | // this.buttonColor = props.buttonColor; 30 | // } 31 | 32 | // render() { 33 | // return ( 34 | // 37 | // {this.props.children} 38 | // 39 | // ); 40 | // } 41 | // } 42 | 43 | // export class DangerButton extends Button { 44 | // constructor(props) { 45 | // super(props); 46 | // this.buttonColor = 'red'; 47 | // } 48 | // } -------------------------------------------------------------------------------- /src/CongratulationsMessage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Button } from './Button'; 3 | 4 | export const CongratulationsMessage = ({ threshold, onHide }) => { 5 | return ( 6 | <> 7 |

Congratulations! You've reached {threshold} clicks

8 | 9 | 10 | ); 11 | } -------------------------------------------------------------------------------- /src/CounterButton.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { DangerButton } from './Button'; 3 | 4 | export const CounterButton = ({ numberOfClicks, onIncrement }) => { 5 | return ( 6 | <> 7 |

You've clicked the button {numberOfClicks} times

8 | Click me! 9 | 10 | ); 11 | } 12 | -------------------------------------------------------------------------------- /src/DisplayIf.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export const DisplayIf = ({ condition, children }) => { 4 | return condition 5 | ? children 6 | : null; 7 | } -------------------------------------------------------------------------------- /src/Greeting.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export const Greeting = ({ name, numberOfMessages }) => { 4 | if (!name) return null; 5 | let isMorning = (new Date()).getHours() < 12; 6 | let greetingHeader = isMorning 7 | ?

Good Morning {name}!

8 | :

Good Evening {name}!

9 | 10 | return ( 11 | <> 12 | {greetingHeader} 13 | {numberOfMessages === 0 14 | ? null 15 | :

You have {numberOfMessages} new messages

} 16 | 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /src/NavBar.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Link } from 'react-router-dom'; 3 | 4 | export const NavBar = ({ links }) => ( 5 | 12 | ); -------------------------------------------------------------------------------- /src/PeopleList.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { PeopleListItem } from './PeopleListItem'; 3 | import styled from 'styled-components'; 4 | 5 | const Wrapper = styled.div` 6 | border: 2px solid white; 7 | padding: 32px; 8 | `; 9 | 10 | export const PeopleList = ({ people }) => ( 11 | 12 | {people.map(person => )} 13 | 14 | ); -------------------------------------------------------------------------------- /src/PeopleListItem.css: -------------------------------------------------------------------------------- 1 | .list-item-container { 2 | border: 2px solid white; 3 | border-radius: 8px; 4 | width: 400px; 5 | margin-top: 16px; 6 | } -------------------------------------------------------------------------------- /src/PeopleListItem.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from 'react'; 2 | import { ThemeContext } from './ThemeContext'; 3 | import './PeopleListItem.css'; 4 | 5 | export const PeopleListItem = ({ person }) => { 6 | const theme = useContext(ThemeContext); 7 | return ( 8 |
14 |

{person.name}

15 |

Age: {person.age}

16 |

Hair Color: {person.hairColor}

17 |
18 | ); 19 | } -------------------------------------------------------------------------------- /src/ThemeContext.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export const ThemeContext = React.createContext('dark'); -------------------------------------------------------------------------------- /src/UserDataLoader.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | 3 | export const UserDataLoader = ({ children }) => { 4 | const [user, setUser] = useState({ name: {} }); 5 | 6 | useEffect(() => { 7 | const fetchUser = async () => { 8 | const response = await fetch('https://randomuser.me/api/'); 9 | const data = await response.json(); 10 | setUser(data.results[0]); 11 | } 12 | 13 | fetchUser(); 14 | }, []); 15 | 16 | return React.Children.map(children, child => 17 | React.cloneElement(child, { user })); 18 | } -------------------------------------------------------------------------------- /src/class-based/CongratulationsMessageCB.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export class CongratulationsMessageCB extends React.Component { 4 | render() { 5 | const { numberOfClicks, threshold, onHide } = this.props; 6 | 7 | return numberOfClicks >= threshold 8 | ? ( 9 | <> 10 |

Congratulations! You've reached {threshold} clicks

11 | 12 | 13 | ) : null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/class-based/CounterButtonCB.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export class CounterButtonCB extends React.Component { 4 | render() { 5 | const { numberOfClicks, onIncrement } = this.props; 6 | return ( 7 | <> 8 |

You've clicked the button {numberOfClicks} times

9 | 10 | 11 | ); 12 | } 13 | } -------------------------------------------------------------------------------- /src/class-based/CounterButtonPageCB.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { CounterButtonCB } from './CounterButtonCB'; 3 | import { CongratulationsMessageCB } from './CongratulationsMessageCB'; 4 | 5 | export class CounterButtonPageCB extends React.Component { 6 | state = { 7 | hideMessage: false, 8 | numberOfClicks: 0, 9 | } 10 | 11 | constructor(props) { 12 | super(props); 13 | this.increment = this.increment.bind(this); 14 | } 15 | 16 | increment() { 17 | this.setState({ numberOfClicks: this.state.numberOfClicks + 1}) 18 | } 19 | 20 | render() { 21 | const { hideMessage, numberOfClicks } = this.state; 22 | 23 | return ( 24 | <> 25 |

The Counter Button Page

26 | {hideMessage 27 | ? null 28 | : this.setState({ hideMessage: true })} />} 32 | 35 | 36 | ); 37 | } 38 | } -------------------------------------------------------------------------------- /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 * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/pages/ControlledFormPage.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { SuccessButton } from '../Button'; 3 | 4 | export const ControlledFormPage = () => { 5 | const [name, setName] = useState(''); 6 | const [email, setEmail] = useState(''); 7 | const [favoriteColor, setFavoriteColor] = useState(''); 8 | 9 | return ( 10 |
11 |

Please enter your information:

12 |
13 | setName(e.target.value)} /> 18 |
19 |
20 | setEmail(e.target.value)} /> 25 |
26 |
27 | setFavoriteColor(e.target.value)} /> 32 |
33 | { 34 | alert(` 35 | Your name is ${name}, 36 | your email is ${email}, 37 | and your favorite color is ${favoriteColor}! 38 | `); 39 | event.preventDefault(); 40 | }}>Submit 41 |
42 | ); 43 | } -------------------------------------------------------------------------------- /src/pages/CounterButtonPage.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { usePersistentState } from '../usePersistentState'; 3 | import { CounterButton } from '../CounterButton'; 4 | import { CongratulationsMessage } from '../CongratulationsMessage'; 5 | import { DisplayIf } from '../DisplayIf'; 6 | 7 | export const CounterButtonPage = ({ match }) => { 8 | const [numberOfClicks, setNumberOfClicks] = usePersistentState( 9 | 'numberOfClicks', 10 | 0, 11 | Number, 12 | ); 13 | const [hideMessage, setHideMessage] = useState(false); 14 | 15 | const increment = () => setNumberOfClicks(numberOfClicks + 1); 16 | 17 | return ( 18 | <> 19 |

The Counter Button Page

20 | = 10}> 21 | setHideMessage(true)} /> 24 | 25 | 28 | 29 | ); 30 | } -------------------------------------------------------------------------------- /src/pages/HomePage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Link } from 'react-router-dom'; 3 | import { Greeting } from '../Greeting'; 4 | 5 | export const HomePage = () => ( 6 | <> 7 |

The Home Page

8 | 9 | Go to Counter Page 10 | Go to People List Page 11 | 12 | ); -------------------------------------------------------------------------------- /src/pages/NotFoundPage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export const NotFoundPage = () => ( 4 |

404: Page Not Found

5 | ); -------------------------------------------------------------------------------- /src/pages/PeopleListPage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { PeopleList } from '../PeopleList'; 3 | 4 | const people = [{ 5 | name: 'John', 6 | age: 40, 7 | hairColor: 'brown', 8 | }, { 9 | name: 'Helga', 10 | age: 25, 11 | hairColor: 'red', 12 | }, { 13 | name: 'Dwayne', 14 | age: 55, 15 | hairColor: 'blonde', 16 | }]; 17 | 18 | export const PeopleListPage = () => ( 19 | <> 20 |

The People List Page

21 | 22 | 23 | ); -------------------------------------------------------------------------------- /src/pages/ProtectedPage.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from 'react'; 2 | // import { Redirect } from 'react-router-dom'; 3 | import { useHistory } from 'react-router-dom'; 4 | 5 | export const ProtectedPage = () => { 6 | const isAuthed = false; 7 | const history = useHistory(); 8 | 9 | useEffect(() => { 10 | if (!isAuthed) { 11 | history.push('/'); 12 | } 13 | }); 14 | 15 | return

Only authed users should see this!

; 16 | 17 | // return isAuthed 18 | // ?

Only authed users should see this!

19 | // : ; 20 | } -------------------------------------------------------------------------------- /src/pages/UncontrolledFormPage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export const UncontrolledFormPage = () => { 4 | const nameInput = React.createRef(); 5 | const emailInput = React.createRef(); 6 | const favoriteColorInput = React.createRef(); 7 | 8 | return ( 9 |
10 |

Please enter your information:

11 |
12 | 16 |
17 |
18 | 22 |
23 |
24 | 28 |
29 | 37 |
38 | ); 39 | } -------------------------------------------------------------------------------- /src/pages/UserProfilePage.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useUserData } from '../useUserData'; 3 | 4 | export const UserProfilePage = () => { 5 | const user = useUserData(); 6 | return ( 7 |
8 |

Name: {user.name.first} {user.name.last}

9 |

Email: {user.email}

10 |
11 | ); 12 | } -------------------------------------------------------------------------------- /src/pages/index.js: -------------------------------------------------------------------------------- 1 | export { ControlledFormPage } from './ControlledFormPage'; 2 | export { CounterButtonPage } from './CounterButtonPage'; 3 | export { HomePage } from './HomePage'; 4 | export { NotFoundPage } from './NotFoundPage'; 5 | export { PeopleListPage } from './PeopleListPage'; 6 | export { ProtectedPage } from './ProtectedPage'; 7 | export { UncontrolledFormPage } from './UncontrolledFormPage'; 8 | export { UserProfilePage } from './UserProfilePage'; -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /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/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/usePersistentState.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | 3 | export const usePersistentState = (storageKey, defaultValue, Type) => { 4 | const [state, setState] = useState( 5 | Type(localStorage.getItem(storageKey)) || defaultValue 6 | ); 7 | 8 | useEffect(() => { 9 | localStorage.setItem(storageKey, state); 10 | }, [state, storageKey]); 11 | 12 | return [state, setState]; 13 | } 14 | -------------------------------------------------------------------------------- /src/useUserData.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | 3 | export const useUserData = () => { 4 | const [user, setUser] = useState({ name: {} }); 5 | 6 | useEffect(() => { 7 | const fetchUser = async () => { 8 | const response = await fetch('https://randomuser.me/api/'); 9 | const data = await response.json(); 10 | setUser(data.results[0]); 11 | } 12 | 13 | fetchUser(); 14 | }, []); 15 | 16 | return user; 17 | } --------------------------------------------------------------------------------