├── .vscode └── settings.json ├── 01-react-just-render ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ └── logo.svg ├── 02-react-input-field ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.js │ ├── App.test.js │ ├── Greeter.js │ ├── index.css │ └── index.js ├── 03-testing-stateless-components ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.js │ ├── App.test.js │ ├── User.js │ ├── User.test.js │ ├── index.css │ └── index.js ├── 03a-todo-list-from-scratch ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json └── src │ ├── AllTodos.js │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── NewTodo.js │ ├── TodoList.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── 04-redux-store ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.js │ ├── User.js │ ├── index.css │ └── index.js ├── 05-redux-input-field ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.js │ ├── App.test.js │ ├── Greeter.js │ ├── Reducer.js │ ├── index.css │ └── index.js ├── 06-testing-redux ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.js │ ├── App.test.js │ ├── Greeter.js │ ├── Greeter.test.js │ ├── GreeterActions.js │ ├── GreeterActions.test.js │ ├── Reducer.js │ ├── Reducer.test.js │ ├── index.css │ └── index.js ├── 07-full-example-todo-list ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── AllTodos.js │ ├── App.test.js │ ├── NewTodo.js │ ├── Reducer.js │ ├── TodoActions.js │ ├── TodoList.js │ ├── index.css │ └── index.js ├── 08-context-and-refs ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json └── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── CollapsibleList.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── 09-hooks ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json └── src │ ├── AllTodos.js │ ├── NewTodo.js │ ├── TodoList.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── 11-reducer-composition ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── AllTodos.js │ ├── NewTodo.js │ ├── Reducer.js │ ├── TodoActions.js │ ├── TodoList.js │ ├── index.css │ └── index.js ├── 12-delayed-result ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── AllTodos.js │ ├── NewTodo.js │ ├── NewTodoActions.js │ ├── NewTodoReducer.js │ ├── Overlay.js │ ├── TabsActions.js │ ├── TabsReducer.js │ ├── TodoList.js │ ├── TodosActions.js │ ├── TodosReducer.js │ ├── index.css │ └── index.js ├── 13-styling-css ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── AllTodos.js │ ├── NewTodo.js │ ├── NewTodoActions.js │ ├── NewTodoReducer.js │ ├── Overlay.js │ ├── TabsActions.js │ ├── TabsReducer.js │ ├── TodoList.js │ ├── TodosActions.js │ ├── TodosReducer.js │ ├── index.css │ └── index.js ├── 14-add-flow-types ├── .flowconfig ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── global.css │ └── index.html └── src │ ├── AllTodos.js │ ├── NewTodo.js │ ├── NewTodoActions.js │ ├── NewTodoReducer.js │ ├── Overlay.css │ ├── Overlay.js │ ├── TabsActions.js │ ├── TabsReducer.js │ ├── TodoList.css │ ├── TodoList.js │ ├── TodosActions.js │ ├── TodosReducer.js │ ├── index.css │ └── index.js ├── 15-component-lifecycle ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── joint.css │ └── style.css └── src │ ├── Header.js │ ├── MainSection.js │ ├── index.js │ ├── stations │ ├── Link.js │ ├── Station.js │ ├── StationsView.js │ └── stationsReducer.js │ └── tabsReducer.js ├── 20-todo-list-from-scratch ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json └── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── LICENSE └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.insertSpaces": true, 3 | "editor.tabSize": 2 4 | } -------------------------------------------------------------------------------- /01-react-just-render/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /01-react-just-render/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "01-react-just-render", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | }, 16 | "browserslist": { 17 | "production": [ 18 | ">0.2%", 19 | "not dead", 20 | "not op_mini all" 21 | ], 22 | "development": [ 23 | "last 1 chrome version", 24 | "last 1 firefox version", 25 | "last 1 safari version" 26 | ] 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /01-react-just-render/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/01-react-just-render/public/favicon.ico -------------------------------------------------------------------------------- /01-react-just-render/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /01-react-just-render/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 80px; 8 | } 9 | 10 | .App-header { 11 | background-color: #222; 12 | height: 150px; 13 | padding: 20px; 14 | color: white; 15 | } 16 | 17 | .App-intro { 18 | font-size: large; 19 | } 20 | 21 | @keyframes App-logo-spin { 22 | from { transform: rotate(0deg); } 23 | to { transform: rotate(360deg); } 24 | } 25 | -------------------------------------------------------------------------------- /01-react-just-render/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | class App extends Component { 6 | render() { 7 | return ( 8 |
9 |
10 | logo 11 |

Welcome to React

12 |
13 |

14 | To get started, edit src/App.js and save to reload. 15 |

16 |
17 | ); 18 | } 19 | } 20 | 21 | export default App; 22 | -------------------------------------------------------------------------------- /01-react-just-render/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /01-react-just-render/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /01-react-just-render/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './index.css'; 5 | 6 | ReactDOM.render( 7 | , 8 | document.getElementById('root') 9 | ); 10 | -------------------------------------------------------------------------------- /01-react-just-render/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /02-react-input-field/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /02-react-input-field/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "02-react-input-field", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | }, 16 | "browserslist": { 17 | "production": [ 18 | ">0.2%", 19 | "not dead", 20 | "not op_mini all" 21 | ], 22 | "development": [ 23 | "last 1 chrome version", 24 | "last 1 firefox version", 25 | "last 1 safari version" 26 | ] 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /02-react-input-field/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/02-react-input-field/public/favicon.ico -------------------------------------------------------------------------------- /02-react-input-field/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /02-react-input-field/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import Greeter from './Greeter' 3 | 4 | class App extends Component { 5 | render() { 6 | return ( 7 | 8 | ); 9 | } 10 | } 11 | 12 | export default App; 13 | -------------------------------------------------------------------------------- /02-react-input-field/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /02-react-input-field/src/Greeter.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | class Greeter extends React.Component { 4 | render() { 5 | return ( 6 |
Hello {this.props.name}
7 | ); 8 | } 9 | } 10 | 11 | export default Greeter; 12 | -------------------------------------------------------------------------------- /02-react-input-field/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /02-react-input-field/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './index.css'; 5 | 6 | ReactDOM.render( 7 | , 8 | document.getElementById('root') 9 | ); 10 | -------------------------------------------------------------------------------- /03-testing-stateless-components/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /03-testing-stateless-components/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "03-testing-stateless-components", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "devDependencies": { 11 | "chai": "^4.2.0", 12 | "chai-enzyme": "^1.0.0-beta.1", 13 | "enzyme": "^3.10.0", 14 | "react-addons-test-utils": "^15.6.2" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test --env=jsdom", 20 | "eject": "react-scripts eject" 21 | }, 22 | "browserslist": { 23 | "production": [ 24 | ">0.2%", 25 | "not dead", 26 | "not op_mini all" 27 | ], 28 | "development": [ 29 | "last 1 chrome version", 30 | "last 1 firefox version", 31 | "last 1 safari version" 32 | ] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /03-testing-stateless-components/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/03-testing-stateless-components/public/favicon.ico -------------------------------------------------------------------------------- /03-testing-stateless-components/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /03-testing-stateless-components/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import User from './User'; 3 | 4 | class App extends Component { 5 | render() { 6 | return ( 7 |
    8 | 9 | 10 |
11 | ); 12 | } 13 | } 14 | 15 | export default App; 16 | -------------------------------------------------------------------------------- /03-testing-stateless-components/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /03-testing-stateless-components/src/User.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | class User extends React.PureComponent { 4 | render() { 5 | const email = this.props.email ? {this.props.email} : null; 6 | const active = this.props.active || false; 7 | const activeInfo = active: {active.toString()}; 8 | 9 | return ( 10 |
  • 11 | {this.props.firstName}  12 | {this.props.lastName} 13 | {email} 14 | {activeInfo} 15 |
  • 16 | ); 17 | } 18 | } 19 | 20 | export default User; 21 | -------------------------------------------------------------------------------- /03-testing-stateless-components/src/User.test.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/03-testing-stateless-components/src/User.test.js -------------------------------------------------------------------------------- /03-testing-stateless-components/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /03-testing-stateless-components/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './index.css'; 5 | 6 | ReactDOM.render( 7 | , 8 | document.getElementById('root') 9 | ); 10 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/.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 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/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 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "03a-todo-list-from-scratch", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test", 14 | "eject": "react-scripts eject" 15 | }, 16 | "eslintConfig": { 17 | "extends": "react-app" 18 | }, 19 | "browserslist": { 20 | "production": [ 21 | ">0.2%", 22 | "not dead", 23 | "not op_mini all" 24 | ], 25 | "development": [ 26 | "last 1 chrome version", 27 | "last 1 firefox version", 28 | "last 1 safari version" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/03a-todo-list-from-scratch/public/favicon.ico -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
    27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/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 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export class AllTodos extends React.Component { 4 | render() { 5 | return ( 6 |
      7 |
    • FIXME render all todos
    • 8 |
    9 | ); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | function App() { 6 | return ( 7 |
    8 |
    9 | logo 10 |

    11 | Edit src/App.js and save to reload. 12 |

    13 | 19 | Learn React 20 | 21 |
    22 |
    23 | ); 24 | } 25 | 26 | export default App; 27 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export class NewTodo extends React.Component { 4 | render() { 5 | return ( 6 |
    7 | "FIXME"} /> 8 | "FIXME"} /> 9 |
    10 | ); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { AllTodos } from './AllTodos'; 3 | import { NewTodo } from './NewTodo'; 4 | 5 | class TodoList extends Component { 6 | render() { 7 | return ( 8 |
    9 |

    My Todos

    10 | 11 | 12 |
    13 | ); 14 | } 15 | } 16 | 17 | export default TodoList; 18 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/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 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import TodoList from './TodoList'; 4 | import './index.css'; 5 | 6 | ReactDOM.render( 7 | , 8 | document.getElementById('root') 9 | ); 10 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /03a-todo-list-from-scratch/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.1/8 is 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 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /04-redux-store/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /04-redux-store/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "04-redux-store", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react-redux": "^7.1.0", 7 | "redux": "^4.0.4", 8 | "react": "^16.8.6", 9 | "react-dom": "^16.8.6", 10 | "react-scripts": "3.0.1" 11 | }, 12 | "scripts": { 13 | "start": "react-scripts start", 14 | "build": "react-scripts build", 15 | "test": "react-scripts test --env=jsdom", 16 | "eject": "react-scripts eject" 17 | }, 18 | "browserslist": { 19 | "production": [ 20 | ">0.2%", 21 | "not dead", 22 | "not op_mini all" 23 | ], 24 | "development": [ 25 | "last 1 chrome version", 26 | "last 1 firefox version", 27 | "last 1 safari version" 28 | ] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /04-redux-store/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/04-redux-store/public/favicon.ico -------------------------------------------------------------------------------- /04-redux-store/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /04-redux-store/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import User from './User'; 3 | 4 | class App extends Component { 5 | render() { 6 | return ( 7 |
      8 | 9 | 10 |
    11 | ); 12 | } 13 | } 14 | 15 | export default App; 16 | -------------------------------------------------------------------------------- /04-redux-store/src/User.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | class User extends React.PureComponent { 4 | render() { 5 | const email = this.props.email ? {this.props.email} : null; 6 | const active = this.props.active || false; 7 | const activeInfo = active: {active.toString()}; 8 | 9 | return ( 10 |
  • 11 | {this.props.firstName}  12 | {this.props.lastName} 13 | {email} 14 | {activeInfo} 15 |
  • 16 | ); 17 | } 18 | } 19 | 20 | export default User; 21 | -------------------------------------------------------------------------------- /04-redux-store/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /04-redux-store/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './index.css'; 5 | 6 | const initialState = { 7 | users: [ 8 | { firstName: 'John', lastName: 'Doe', email: 'john.doe@example.com', active: 'true'}, 9 | { firstName: 'Jane', lastName: 'Doe' }, 10 | { firstName: 'Mike', lastName: 'Nobody' }, 11 | { firstName: 'Mary', lastName: 'Sims' }, 12 | ] 13 | }; 14 | 15 | ReactDOM.render( 16 | , 17 | document.getElementById('root') 18 | ); 19 | -------------------------------------------------------------------------------- /05-redux-input-field/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /05-redux-input-field/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "05-redux-input-field", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react-redux": "^7.1.0", 7 | "redux": "^4.0.4", 8 | "react": "^16.8.6", 9 | "react-dom": "^16.8.6", 10 | "react-scripts": "3.0.1" 11 | }, 12 | "scripts": { 13 | "start": "react-scripts start", 14 | "build": "react-scripts build", 15 | "test": "react-scripts test --env=jsdom", 16 | "eject": "react-scripts eject" 17 | }, 18 | "browserslist": { 19 | "production": [ 20 | ">0.2%", 21 | "not dead", 22 | "not op_mini all" 23 | ], 24 | "development": [ 25 | "last 1 chrome version", 26 | "last 1 firefox version", 27 | "last 1 safari version" 28 | ] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /05-redux-input-field/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/05-redux-input-field/public/favicon.ico -------------------------------------------------------------------------------- /05-redux-input-field/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /05-redux-input-field/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import Greeter from './Greeter' 3 | 4 | class App extends Component { 5 | render() { 6 | return ( 7 | 8 | ); 9 | } 10 | } 11 | 12 | export default App; 13 | -------------------------------------------------------------------------------- /05-redux-input-field/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /05-redux-input-field/src/Greeter.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | class Greeter extends React.Component { 4 | constructor(props) { 5 | super(props); 6 | this.state = { 7 | toGreet: "", 8 | greeting: "" 9 | }; 10 | } 11 | 12 | render() { 13 | return ( 14 |
    15 | this.setGreeting(e.target.value) }/> 16 |

    {this.state.greeting}

    17 |
    18 | ); 19 | } 20 | 21 | setGreeting(toGreet) { 22 | const greeting = toGreet ? "Hello " + toGreet : ""; 23 | this.setState({ 24 | toGreet, 25 | greeting 26 | }); 27 | } 28 | } 29 | 30 | export default Greeter; 31 | -------------------------------------------------------------------------------- /05-redux-input-field/src/Reducer.js: -------------------------------------------------------------------------------- 1 | export function reducer(state, action) { 2 | switch(action.type) { 3 | 4 | } 5 | return state; 6 | } -------------------------------------------------------------------------------- /05-redux-input-field/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /05-redux-input-field/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './index.css'; 5 | import { createStore } from 'redux'; 6 | import { Provider } from 'react-redux'; 7 | import { reducer } from './Reducer'; 8 | 9 | const initialState = { 10 | welcome: { 11 | toGreet: "", 12 | greeting: "" 13 | } 14 | }; 15 | const store = createStore(reducer, initialState); 16 | 17 | ReactDOM.render( 18 | , 19 | document.getElementById('root') 20 | ); 21 | -------------------------------------------------------------------------------- /06-testing-redux/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /06-testing-redux/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "06-testing-redux", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react-redux": "^7.1.0", 7 | "redux": "^4.0.4", 8 | "react": "^16.8.6", 9 | "react-dom": "^16.8.6", 10 | "react-scripts": "3.0.1", 11 | "redux-logger": "^3.0.6" 12 | }, 13 | "devDependencies": { 14 | "chai": "^4.2.0", 15 | "chai-enzyme": "^1.0.0-beta.1", 16 | "enzyme": "^3.10.0", 17 | "enzyme-redux": "^0.2.1", 18 | "react-addons-test-utils": "^15.6.2" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test --env=jsdom", 24 | "eject": "react-scripts eject" 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /06-testing-redux/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/06-testing-redux/public/favicon.ico -------------------------------------------------------------------------------- /06-testing-redux/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /06-testing-redux/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { GreeterContainer } from './Greeter' 3 | 4 | class App extends Component { 5 | render() { 6 | return ( 7 | 8 | ); 9 | } 10 | } 11 | 12 | export default App; 13 | -------------------------------------------------------------------------------- /06-testing-redux/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /06-testing-redux/src/Greeter.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import GreeterActions from './GreeterActions'; 5 | 6 | export class Greeter extends React.Component { 7 | render() { 8 | return ( 9 |
    10 | 11 |

    {this.props.greeting}

    12 |
    13 | ); 14 | } 15 | } 16 | 17 | function mapStateToProperties(state) { 18 | return { 19 | toGreet: state.welcome.toGreet, 20 | greeting: state.welcome.greeting 21 | }; 22 | } 23 | export const actionCreators = { 24 | toGreetChanged: (event) => { return { type: GreeterActions.greetingChanged, toGreet: event.target.value}; } 25 | } 26 | 27 | export const GreeterContainer = connect(mapStateToProperties, actionCreators)(Greeter); 28 | -------------------------------------------------------------------------------- /06-testing-redux/src/Greeter.test.js: -------------------------------------------------------------------------------- 1 | import { Greeter, GreeterContainer } from './Greeter'; 2 | 3 | describe('', () => { 4 | it('...', () => { 5 | 6 | }); 7 | }); 8 | 9 | describe('', () => { 10 | it('...', () => { 11 | 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /06-testing-redux/src/GreeterActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | greetingChanged: Symbol("GREETING_CHANGED") 3 | }; 4 | -------------------------------------------------------------------------------- /06-testing-redux/src/GreeterActions.test.js: -------------------------------------------------------------------------------- 1 | import { actionCreators } from './Greeter'; 2 | 3 | describe('Action creators for ', () => { 4 | it('...', () => { 5 | 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /06-testing-redux/src/Reducer.js: -------------------------------------------------------------------------------- 1 | import GreeterActions from './GreeterActions'; 2 | 3 | export function reducer(state, action) { 4 | switch(action.type) { 5 | case GreeterActions.greetingChanged: 6 | const greeting = action.toGreet ? "Hello "+action.toGreet : ""; 7 | return { 8 | welcome: { 9 | toGreet: action.toGreet, 10 | greeting: greeting 11 | } 12 | }; 13 | default: 14 | return state; 15 | } 16 | } -------------------------------------------------------------------------------- /06-testing-redux/src/Reducer.test.js: -------------------------------------------------------------------------------- 1 | import { reducer } from './Reducer'; 2 | 3 | describe('Main Reducer', () => { 4 | it('...', () => { 5 | 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /06-testing-redux/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /06-testing-redux/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './index.css'; 5 | import { createStore } from 'redux'; 6 | import { Provider } from 'react-redux'; 7 | import { reducer } from './Reducer'; 8 | 9 | const initialState = { 10 | welcome: { 11 | toGreet: "", 12 | greeting: "" 13 | } 14 | }; 15 | const store = createStore(reducer, initialState); 16 | 17 | ReactDOM.render( 18 | , 19 | document.getElementById('root') 20 | ); 21 | -------------------------------------------------------------------------------- /07-full-example-todo-list/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /07-full-example-todo-list/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "07-full-example-todo-list", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react-redux": "^7.1.0", 7 | "redux": "^4.0.4", 8 | "react": "^16.8.6", 9 | "react-dom": "^16.8.6", 10 | "react-scripts": "3.0.1", 11 | "redux-logger": "^3.0.6" 12 | }, 13 | "devDependencies": { 14 | "chai": "^4.2.0", 15 | "chai-enzyme": "^1.0.0-beta.1", 16 | "enzyme": "^3.10.0", 17 | "enzyme-redux": "^0.2.1", 18 | "react-addons-test-utils": "^15.6.2" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test --env=jsdom", 24 | "eject": "react-scripts eject" 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /07-full-example-todo-list/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/07-full-example-todo-list/public/favicon.ico -------------------------------------------------------------------------------- /07-full-example-todo-list/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodoActions from './TodoActions'; 5 | 6 | export class AllTodos extends React.Component { 7 | render() { 8 | return ( 9 |
      10 |
    • FIXME render all todos
    • 11 |
    12 | ); 13 | } 14 | } 15 | 16 | function mapStateToProperties(state) { 17 | return { 18 | 19 | }; 20 | } 21 | export const actionCreators = { 22 | 23 | } 24 | 25 | export const AllTodosContainer = connect(mapStateToProperties, actionCreators)(AllTodos); 26 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodoActions from './TodoActions'; 5 | 6 | export class NewTodo extends React.Component { 7 | render() { 8 | return ( 9 |
    10 | "FIXME"} /> 11 | "FIXME"} /> 12 |
    13 | ); 14 | } 15 | } 16 | 17 | function mapStateToProperties(state) { 18 | return { 19 | 20 | }; 21 | } 22 | export const actionCreators = { 23 | 24 | } 25 | 26 | export const NewTodoContainer = connect(mapStateToProperties, actionCreators)(NewTodo); 27 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/Reducer.js: -------------------------------------------------------------------------------- 1 | import TodoActions from './TodoActions'; 2 | 3 | export function reducer(state, action) { 4 | switch(action.type) { 5 | case TodoActions.newTodoCreated: 6 | return state; 7 | default: 8 | return state; 9 | } 10 | } -------------------------------------------------------------------------------- /07-full-example-todo-list/src/TodoActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED") 3 | }; 4 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { AllTodosContainer } from './AllTodos'; 3 | import { NewTodoContainer } from './NewTodo'; 4 | 5 | class TodoList extends Component { 6 | render() { 7 | return ( 8 |
    9 |

    My Todos

    10 | 11 | 12 |
    13 | ); 14 | } 15 | } 16 | 17 | export default TodoList; 18 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /07-full-example-todo-list/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import TodoList from './TodoList'; 4 | import './index.css'; 5 | import { createStore } from 'redux'; 6 | import { Provider } from 'react-redux'; 7 | import { reducer } from './Reducer'; 8 | 9 | const initialState = { 10 | todos: [ 11 | { text: "Buy Milk", done: true }, 12 | { text: "Buy Beer", done: true }, 13 | { text: "Write workshop examples", done: false}, 14 | { text: "Host workshop", done: false } 15 | ] 16 | }; 17 | const store = createStore(reducer, initialState); 18 | 19 | ReactDOM.render( 20 | , 21 | document.getElementById('root') 22 | ); 23 | -------------------------------------------------------------------------------- /08-context-and-refs/.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 | -------------------------------------------------------------------------------- /08-context-and-refs/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 | -------------------------------------------------------------------------------- /08-context-and-refs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "08-context-and-refs", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test", 14 | "eject": "react-scripts eject" 15 | }, 16 | "eslintConfig": { 17 | "extends": "react-app" 18 | }, 19 | "browserslist": { 20 | "production": [ 21 | ">0.2%", 22 | "not dead", 23 | "not op_mini all" 24 | ], 25 | "development": [ 26 | "last 1 chrome version", 27 | "last 1 firefox version", 28 | "last 1 safari version" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /08-context-and-refs/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/08-context-and-refs/public/favicon.ico -------------------------------------------------------------------------------- /08-context-and-refs/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
    27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /08-context-and-refs/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 | -------------------------------------------------------------------------------- /08-context-and-refs/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /08-context-and-refs/src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './App.css'; 3 | import { CollapsibleList } from './CollapsibleList'; 4 | 5 | class App extends React.PureComponent { 6 | render() { 7 | return ( 8 |
    9 |

    10 |
      11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
    21 |
    22 | ); 23 | } 24 | } 25 | 26 | export default App; 27 | -------------------------------------------------------------------------------- /08-context-and-refs/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /08-context-and-refs/src/CollapsibleList.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './App.css'; 3 | 4 | export class CollapsibleList extends React.PureComponent { 5 | constructor(props) { 6 | super(props); 7 | this.state = { 8 | collapsed: true, 9 | }; 10 | } 11 | render() { 12 | return ( 13 |
  • 14 | this.setState({collapsed: !this.state.collapsed})}> 15 | {this.state.collapsed? '+' : '-'} 16 | 17 | {this.props.items[this.props.items.length-1]} 18 | {this._subList()} 19 |
  • 20 | ); 21 | } 22 | _subList() { 23 | if(!this.state.collapsed) { 24 | return ( 25 |
      26 | { this.props.items.map(item =>
    • {item}
    • )} 27 |
    28 | ); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /08-context-and-refs/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 | -------------------------------------------------------------------------------- /08-context-and-refs/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(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /08-context-and-refs/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /08-context-and-refs/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.1/8 is 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 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /09-hooks/.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 | -------------------------------------------------------------------------------- /09-hooks/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 | -------------------------------------------------------------------------------- /09-hooks/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "09-hooks", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test", 14 | "eject": "react-scripts eject" 15 | }, 16 | "eslintConfig": { 17 | "extends": "react-app" 18 | }, 19 | "browserslist": { 20 | "production": [ 21 | ">0.2%", 22 | "not dead", 23 | "not op_mini all" 24 | ], 25 | "development": [ 26 | "last 1 chrome version", 27 | "last 1 firefox version", 28 | "last 1 safari version" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /09-hooks/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/09-hooks/public/favicon.ico -------------------------------------------------------------------------------- /09-hooks/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
    27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /09-hooks/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 | -------------------------------------------------------------------------------- /09-hooks/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export class AllTodos extends React.Component { 4 | render() { 5 | return ( 6 |
      7 | {this.props.todos.map(todo => ( 8 |
    • {todo.text}
    • 9 | ))} 10 |
    11 | ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /09-hooks/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export class NewTodo extends React.Component { 4 | constructor(props) { 5 | super(props); 6 | this.state = { 7 | input: "", 8 | }; 9 | } 10 | render() { 11 | return ( 12 |
    13 | this.setState({input: e.target.value})} /> 14 | { 15 | this.props.create(this.state.input); 16 | this.setState({input: ""}); 17 | }} /> 18 |
    19 | ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /09-hooks/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { AllTodos } from './AllTodos'; 3 | import { NewTodo } from './NewTodo'; 4 | 5 | class TodoList extends Component { 6 | constructor(props) { 7 | super(props); 8 | this.state = { 9 | todos: [], 10 | }; 11 | } 12 | 13 | addTodo(todo) { 14 | this.setState({ 15 | todos: [ 16 | ...this.state.todos, 17 | { text: todo, key: Date.now() }, 18 | ], 19 | }); 20 | } 21 | 22 | render() { 23 | return ( 24 |
    25 |

    My Todos

    26 | 27 | this.addTodo(todo) }/> 28 |
    29 | ); 30 | } 31 | } 32 | 33 | export default TodoList; 34 | -------------------------------------------------------------------------------- /09-hooks/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 | -------------------------------------------------------------------------------- /09-hooks/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import TodoList from './TodoList'; 4 | import './index.css'; 5 | 6 | ReactDOM.render( 7 | , 8 | document.getElementById('root') 9 | ); 10 | -------------------------------------------------------------------------------- /09-hooks/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /09-hooks/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.1/8 is 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 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /11-reducer-composition/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /11-reducer-composition/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "11-reducer-composition", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react-redux": "^7.1.0", 7 | "redux": "^4.0.4", 8 | "react": "^16.8.6", 9 | "react-dom": "^16.8.6", 10 | "react-scripts": "3.0.1", 11 | "redux-logger": "^3.0.6" 12 | }, 13 | "devDependencies": { 14 | "chai": "^4.2.0", 15 | "chai-enzyme": "^1.0.0-beta.1", 16 | "enzyme": "^3.10.0", 17 | "enzyme-redux": "^0.2.1", 18 | "react-addons-test-utils": "^15.6.2" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test --env=jsdom", 24 | "eject": "react-scripts eject" 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /11-reducer-composition/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/11-reducer-composition/public/favicon.ico -------------------------------------------------------------------------------- /11-reducer-composition/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /11-reducer-composition/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodoActions from './TodoActions'; 5 | 6 | export class AllTodos extends React.Component { 7 | render() { 8 | return ( 9 |
      10 | {this.props.todos.map(this.renderTodo.bind(this))} 11 |
    12 | ); 13 | } 14 | 15 | renderTodo(todoData) { 16 | return ( 17 |
  • 18 | this.props.toggleTodoState(todoData.text) } /> 19 | {todoData.text} 20 |
  • ); 21 | } 22 | } 23 | 24 | function mapStateToProperties(state) { 25 | return { 26 | todos: state.todos 27 | }; 28 | } 29 | export const actionCreators = { 30 | toggleTodoState: (text) => { return { type: TodoActions.todoStateToggled, text: text }} 31 | } 32 | 33 | export const AllTodosContainer = connect(mapStateToProperties, actionCreators)(AllTodos); 34 | -------------------------------------------------------------------------------- /11-reducer-composition/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodoActions from './TodoActions'; 5 | 6 | export class NewTodo extends React.Component { 7 | render() { 8 | return ( 9 |
    10 | 11 | this.props.newTodoSubmitted(this.props.newText)} /> 12 |
    13 | ); 14 | } 15 | } 16 | 17 | function mapStateToProperties(state) { 18 | return { 19 | newText: state.newTodo.text 20 | }; 21 | } 22 | export const actionCreators = { 23 | inputTextChanged: (event) => { return { type: TodoActions.newTodoTextChanged, text: event.target.value }}, 24 | newTodoSubmitted: (text) => { return { type: TodoActions.newTodoCreated, text: text }} 25 | } 26 | 27 | export const NewTodoContainer = connect(mapStateToProperties, actionCreators)(NewTodo); 28 | -------------------------------------------------------------------------------- /11-reducer-composition/src/Reducer.js: -------------------------------------------------------------------------------- 1 | import TodoActions from './TodoActions'; 2 | 3 | export function reducer(state, action) { 4 | switch(action.type) { 5 | case TodoActions.todoStateToggled: 6 | const newTodos = state.todos.map(todo => { 7 | const newTodo = Object.assign({}, todo); 8 | if(newTodo.text === action.text) { 9 | newTodo.done = !newTodo.done; 10 | return newTodo; 11 | } 12 | return newTodo; 13 | }); 14 | return Object.assign({}, state, { 15 | todos: newTodos 16 | }); 17 | case TodoActions.newTodoCreated: 18 | return Object.assign({}, state, { 19 | todos: [ 20 | ...state.todos, 21 | { text: action.text, done: false } 22 | ], 23 | newTodo: { text: "" } 24 | }); 25 | case TodoActions.newTodoTextChanged: 26 | return Object.assign({}, state, { 27 | newTodo: { text: action.text } 28 | }); 29 | case TodoActions.tabSwitched: 30 | return Object.assign({}, state, { 31 | tabs: { activeTab: action.tab } 32 | }); 33 | default: 34 | return state; 35 | } 36 | } -------------------------------------------------------------------------------- /11-reducer-composition/src/TodoActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /11-reducer-composition/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodoActions from './TodoActions'; 5 | import { AllTodosContainer } from './AllTodos'; 6 | import { NewTodoContainer } from './NewTodo'; 7 | 8 | export class TodoList extends Component { 9 | render() { 10 | const todosTab = ( 11 |
    12 |

    My Todos

    13 | 14 |
    15 | ); 16 | const newTodoTab = ( 17 |
    18 |

    New Todo

    19 | 20 |
    21 | ); 22 | const activeTab = this.props.activeTab==="all"? todosTab : newTodoTab; 23 | return ( 24 | 31 | ); 32 | } 33 | } 34 | 35 | function mapStateToProperties(state) { 36 | return { 37 | activeTab: state.tabs.activeTab 38 | }; 39 | } 40 | export const actionCreators = { 41 | switchTab: (tab) => { return { type: TodoActions.tabSwitched, tab: tab }} 42 | } 43 | 44 | export const TodoListContainer = connect(mapStateToProperties, actionCreators)(TodoList); 45 | -------------------------------------------------------------------------------- /11-reducer-composition/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /11-reducer-composition/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { TodoListContainer } from './TodoList'; 4 | import './index.css'; 5 | import { createStore } from 'redux'; 6 | import { Provider } from 'react-redux'; 7 | import { reducer } from './Reducer'; 8 | 9 | const initialState = { 10 | todos: [ 11 | { text: "Buy Milk", done: true }, 12 | { text: "Buy Beer", done: true }, 13 | { text: "Write workshop examples", done: false}, 14 | { text: "Host workshop", done: false } 15 | ], 16 | newTodo: { text: "" }, 17 | tabs: { activeTab: "all" } 18 | }; 19 | const store = createStore(reducer, initialState); 20 | 21 | ReactDOM.render( 22 | , 23 | document.getElementById('root') 24 | ); 25 | -------------------------------------------------------------------------------- /12-delayed-result/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /12-delayed-result/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "12-delayed-result", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "redux-thunk": "^2.3.0", 7 | "react-redux": "^7.1.0", 8 | "redux": "^4.0.4", 9 | "react": "^16.8.6", 10 | "react-dom": "^16.8.6", 11 | "react-scripts": "3.0.1", 12 | "redux-logger": "^3.0.6" 13 | }, 14 | "devDependencies": { 15 | "chai": "^4.2.0", 16 | "chai-enzyme": "^1.0.0-beta.1", 17 | "enzyme": "^3.10.0", 18 | "enzyme-redux": "^0.2.1", 19 | "react-addons-test-utils": "^15.6.2" 20 | }, 21 | "scripts": { 22 | "start": "react-scripts start", 23 | "build": "react-scripts build", 24 | "test": "react-scripts test --env=jsdom", 25 | "eject": "react-scripts eject" 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 | -------------------------------------------------------------------------------- /12-delayed-result/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/12-delayed-result/public/favicon.ico -------------------------------------------------------------------------------- /12-delayed-result/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /12-delayed-result/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodosActions from './TodosActions'; 5 | 6 | export class AllTodos extends React.Component { 7 | render() { 8 | return ( 9 |
      10 | {this.props.todos.map(this.renderTodo.bind(this))} 11 |
    12 | ); 13 | } 14 | 15 | renderTodo(todoData) { 16 | return ( 17 |
  • 18 | this.props.toggleTodoState(todoData.text) } /> 19 | {todoData.text} 20 |
  • ); 21 | } 22 | } 23 | 24 | function mapStateToProperties(state) { 25 | return { 26 | todos: state.todos.all 27 | }; 28 | } 29 | export const actionCreators = { 30 | toggleTodoState: (text) => { return { type: TodosActions.todoStateToggled, text: text }} 31 | } 32 | 33 | export const AllTodosContainer = connect(mapStateToProperties, actionCreators)(AllTodos); 34 | -------------------------------------------------------------------------------- /12-delayed-result/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import NewTodoActions from './NewTodoActions'; 5 | 6 | export class NewTodo extends React.Component { 7 | render() { 8 | return ( 9 |
    10 | 11 | this.props.newTodoSubmitted(this.props.newText)} /> 12 |
    13 | ); 14 | } 15 | } 16 | 17 | function createTodo(text) { 18 | return { text: text, done: false }; 19 | } 20 | 21 | function createTodoOnServer(text, callback) { 22 | //This function simulates the delay caused by creating a TODO on 23 | //a server. It just uses setTimeout instead of a real server communication. 24 | setTimeout(() => callback(createTodo(text)), 2000); 25 | } 26 | 27 | function mapStateToProperties(state) { 28 | return { 29 | newText: state.newTodo.text 30 | }; 31 | } 32 | export const actionCreators = { 33 | inputTextChanged: (event) => { return { type: NewTodoActions.newTodoTextChanged, text: event.target.value }}, 34 | newTodoSubmitted: (text) => { return { type: NewTodoActions.newTodoCreated, todo: createTodo(text) }} 35 | } 36 | 37 | export const NewTodoContainer = connect(mapStateToProperties, actionCreators)(NewTodo); 38 | -------------------------------------------------------------------------------- /12-delayed-result/src/NewTodoActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /12-delayed-result/src/NewTodoReducer.js: -------------------------------------------------------------------------------- 1 | import NewTodoActions from './NewTodoActions'; 2 | 3 | const initialState = { text: "" }; 4 | 5 | export function newTodoReducer(state = initialState, action) { 6 | switch(action.type) { 7 | case NewTodoActions.newTodoCreated: 8 | return Object.assign({}, state, { 9 | text: "" 10 | }); 11 | case NewTodoActions.newTodoTextChanged: 12 | return Object.assign({}, state, { 13 | text: action.text 14 | }); 15 | default: 16 | return state; 17 | } 18 | } -------------------------------------------------------------------------------- /12-delayed-result/src/Overlay.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | export class Overlay extends React.Component { 5 | render() { 6 | const display = this.props.overlay? "block" : "none"; 7 | const style = { 8 | background: "rgba(0, 0, 0, 0.5)", 9 | display: display, 10 | position: "absolute", 11 | top: 0, bottom: 0, left: 0, right: 0, 12 | zIndex: 1000 13 | }; 14 | return ( 15 |
    16 |
    17 | ); 18 | } 19 | } 20 | 21 | function mapStateToProperties(state) { 22 | return { 23 | overlay: state.tabs.overlay 24 | }; 25 | } 26 | 27 | export const OverlayContainer = connect(mapStateToProperties)(Overlay); 28 | -------------------------------------------------------------------------------- /12-delayed-result/src/TabsActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /12-delayed-result/src/TabsReducer.js: -------------------------------------------------------------------------------- 1 | import TabsActions from './TabsActions'; 2 | 3 | const initialState = { activeTab: "all" }; 4 | 5 | export function tabsReducer(state = initialState, action) { 6 | switch(action.type) { 7 | case TabsActions.tabSwitched: 8 | return Object.assign({}, state, { 9 | activeTab: action.tab 10 | }); 11 | default: 12 | return state; 13 | } 14 | } -------------------------------------------------------------------------------- /12-delayed-result/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TabsActions from './TabsActions'; 5 | import { AllTodosContainer } from './AllTodos'; 6 | import { NewTodoContainer } from './NewTodo'; 7 | 8 | export class TodoList extends Component { 9 | render() { 10 | const todosTab = ( 11 |
    12 |

    My Todos

    13 | 14 |
    15 | ); 16 | const newTodoTab = ( 17 |
    18 |

    New Todo

    19 | 20 |
    21 | ); 22 | const activeTab = this.props.activeTab==="all"? todosTab : newTodoTab; 23 | return ( 24 | 31 | ); 32 | } 33 | } 34 | 35 | function mapStateToProperties(state) { 36 | return { 37 | activeTab: state.tabs.activeTab 38 | }; 39 | } 40 | export const actionCreators = { 41 | switchTab: (tab) => { return { type: TabsActions.tabSwitched, tab: tab }} 42 | } 43 | 44 | export const TodoListContainer = connect(mapStateToProperties, actionCreators)(TodoList); 45 | -------------------------------------------------------------------------------- /12-delayed-result/src/TodosActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /12-delayed-result/src/TodosReducer.js: -------------------------------------------------------------------------------- 1 | import TodosActions from './TodosActions'; 2 | import NewTodoActions from './NewTodoActions'; 3 | 4 | const initialState = { 5 | all: [ 6 | { text: "Buy Milk", done: true }, 7 | { text: "Buy Beer", done: true }, 8 | { text: "Write workshop examples", done: false}, 9 | { text: "Host workshop", done: false } 10 | ] 11 | }; 12 | export function todosReducer(state = initialState, action) { 13 | switch(action.type) { 14 | case TodosActions.todoStateToggled: 15 | const newTodos = state.all.map(todo => { 16 | const newTodo = Object.assign({}, todo); 17 | if(newTodo.text === action.text) { 18 | newTodo.done = !newTodo.done; 19 | return newTodo; 20 | } 21 | return newTodo; 22 | }); 23 | return Object.assign({}, state, { 24 | all: newTodos 25 | }); 26 | case NewTodoActions.newTodoCreated: 27 | return Object.assign({}, state, { 28 | all: [ 29 | ...state.all, 30 | action.todo 31 | ] 32 | }); 33 | default: 34 | return state; 35 | } 36 | } -------------------------------------------------------------------------------- /12-delayed-result/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /12-delayed-result/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { TodoListContainer } from './TodoList'; 4 | import './index.css'; 5 | import { createStore, combineReducers } from 'redux'; 6 | import { Provider } from 'react-redux'; 7 | import { tabsReducer } from './TabsReducer'; 8 | import { newTodoReducer } from './NewTodoReducer'; 9 | import { todosReducer } from './TodosReducer'; 10 | 11 | const reducer = combineReducers({ 12 | todos: todosReducer, 13 | newTodo: newTodoReducer, 14 | tabs: tabsReducer 15 | }); 16 | const store = createStore(reducer); 17 | 18 | ReactDOM.render( 19 | , 20 | document.getElementById('root') 21 | ); 22 | -------------------------------------------------------------------------------- /13-styling-css/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /13-styling-css/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "13-styling-css", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "redux-thunk": "^2.3.0", 7 | "react-redux": "^7.1.0", 8 | "redux": "^4.0.4", 9 | "react": "^16.8.6", 10 | "react-dom": "^16.8.6", 11 | "react-scripts": "3.0.1", 12 | "redux-logger": "^3.0.6" 13 | }, 14 | "devDependencies": { 15 | "chai": "^4.2.0", 16 | "chai-enzyme": "^1.0.0-beta.1", 17 | "enzyme": "^3.10.0", 18 | "enzyme-redux": "^0.2.1", 19 | "react-addons-test-utils": "^15.6.2" 20 | }, 21 | "scripts": { 22 | "start": "react-scripts start", 23 | "build": "react-scripts build", 24 | "test": "react-scripts test --env=jsdom", 25 | "eject": "react-scripts eject" 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 | -------------------------------------------------------------------------------- /13-styling-css/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/13-styling-css/public/favicon.ico -------------------------------------------------------------------------------- /13-styling-css/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 16 | React App 17 | 18 | 19 |
    20 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /13-styling-css/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodosActions from './TodosActions'; 5 | 6 | export class AllTodos extends React.Component { 7 | render() { 8 | return ( 9 |
      10 | {this.props.todos.map(this.renderTodo.bind(this))} 11 |
    12 | ); 13 | } 14 | 15 | renderTodo(todoData) { 16 | return ( 17 |
  • 18 | this.props.toggleTodoState(todoData.text) } /> 19 | {todoData.text} 20 |
  • ); 21 | } 22 | } 23 | 24 | function mapStateToProperties(state) { 25 | return { 26 | todos: state.todos.all 27 | }; 28 | } 29 | export const actionCreators = { 30 | toggleTodoState: (text) => { return { type: TodosActions.todoStateToggled, text: text }} 31 | } 32 | 33 | export const AllTodosContainer = connect(mapStateToProperties, actionCreators)(AllTodos); 34 | -------------------------------------------------------------------------------- /13-styling-css/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import NewTodoActions from './NewTodoActions'; 5 | 6 | export class NewTodo extends React.Component { 7 | render() { 8 | return ( 9 |
    10 | 11 | this.props.newTodoSubmitted(this.props.newText)} /> 12 |
    13 | ); 14 | } 15 | } 16 | 17 | function createTodo(text) { 18 | return { text: text, done: false }; 19 | } 20 | 21 | function createTodoOnServer(text, callback) { 22 | //This function simulates the delay caused by creating a TODO on 23 | //a server. It just uses setTimeout instead of a real server communication. 24 | setTimeout(() => callback(createTodo(text)), 2000); 25 | } 26 | 27 | function createNewTodo(text) { 28 | return (dispatch) => { 29 | dispatch({type: NewTodoActions.creatingNewTodo }); 30 | createTodoOnServer(text, (todo) => dispatch({ type: NewTodoActions.newTodoCreated, todo: todo })) 31 | } 32 | } 33 | 34 | function mapStateToProperties(state) { 35 | return { 36 | newText: state.newTodo.text 37 | }; 38 | } 39 | export const actionCreators = { 40 | inputTextChanged: (event) => { return { type: NewTodoActions.newTodoTextChanged, text: event.target.value }}, 41 | newTodoSubmitted: (text) => createNewTodo(text) 42 | } 43 | 44 | export const NewTodoContainer = connect(mapStateToProperties, actionCreators)(NewTodo); 45 | -------------------------------------------------------------------------------- /13-styling-css/src/NewTodoActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | creatingNewTodo: Symbol("CREATING_NEW_TODO") 5 | }; 6 | -------------------------------------------------------------------------------- /13-styling-css/src/NewTodoReducer.js: -------------------------------------------------------------------------------- 1 | import NewTodoActions from './NewTodoActions'; 2 | 3 | const initialState = { text: "" }; 4 | 5 | export function newTodoReducer(state = initialState, action) { 6 | switch(action.type) { 7 | case NewTodoActions.creatingNewTodo: 8 | return Object.assign({}, state, { 9 | text: "" 10 | }); 11 | case NewTodoActions.newTodoTextChanged: 12 | return Object.assign({}, state, { 13 | text: action.text 14 | }); 15 | default: 16 | return state; 17 | } 18 | } -------------------------------------------------------------------------------- /13-styling-css/src/Overlay.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | export class Overlay extends React.Component { 5 | render() { 6 | const display = this.props.overlay? "block" : "none"; 7 | const style = { 8 | background: "rgba(0, 0, 0, 0.5)", 9 | display: display, 10 | position: "absolute", 11 | top: 0, bottom: 0, left: 0, right: 0, 12 | zIndex: 1000 13 | }; 14 | return ( 15 |
    16 |
    17 | ); 18 | } 19 | } 20 | 21 | function mapStateToProperties(state) { 22 | return { 23 | overlay: state.tabs.overlay 24 | }; 25 | } 26 | 27 | export const OverlayContainer = connect(mapStateToProperties)(Overlay); 28 | -------------------------------------------------------------------------------- /13-styling-css/src/TabsActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /13-styling-css/src/TabsReducer.js: -------------------------------------------------------------------------------- 1 | import TabsActions from './TabsActions'; 2 | import NewTodoActions from './NewTodoActions'; 3 | 4 | const initialState = { activeTab: "all", overlay: false }; 5 | 6 | export function tabsReducer(state = initialState, action) { 7 | switch(action.type) { 8 | case TabsActions.tabSwitched: 9 | return Object.assign({}, state, { 10 | activeTab: action.tab 11 | }); 12 | case NewTodoActions.creatingNewTodo: 13 | return Object.assign({}, state, { 14 | overlay: true 15 | }); 16 | case NewTodoActions.newTodoCreated: 17 | return Object.assign({}, state, { 18 | activeTab: "all", 19 | overlay: false 20 | }); 21 | default: 22 | return state; 23 | } 24 | } -------------------------------------------------------------------------------- /13-styling-css/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TabsActions from './TabsActions'; 5 | import { AllTodosContainer } from './AllTodos'; 6 | import { NewTodoContainer } from './NewTodo'; 7 | import { OverlayContainer } from './Overlay'; 8 | 9 | export class TodoList extends Component { 10 | render() { 11 | const todosTab = ( 12 |
    13 |

    My Todos

    14 | 15 |
    16 | ); 17 | const newTodoTab = ( 18 |
    19 |

    New Todo

    20 | 21 |
    22 | ); 23 | const activeTab = this.props.activeTab==="all"? todosTab : newTodoTab; 24 | return ( 25 | 33 | ); 34 | } 35 | } 36 | 37 | function mapStateToProperties(state) { 38 | return { 39 | activeTab: state.tabs.activeTab 40 | }; 41 | } 42 | export const actionCreators = { 43 | switchTab: (tab) => { return { type: TabsActions.tabSwitched, tab: tab }} 44 | } 45 | 46 | export const TodoListContainer = connect(mapStateToProperties, actionCreators)(TodoList); 47 | -------------------------------------------------------------------------------- /13-styling-css/src/TodosActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /13-styling-css/src/TodosReducer.js: -------------------------------------------------------------------------------- 1 | import TodosActions from './TodosActions'; 2 | import NewTodoActions from './NewTodoActions'; 3 | 4 | const initialState = { 5 | all: [ 6 | { text: "Buy Milk", done: true }, 7 | { text: "Buy Beer", done: true }, 8 | { text: "Write workshop examples", done: false}, 9 | { text: "Host workshop", done: false } 10 | ] 11 | }; 12 | export function todosReducer(state = initialState, action) { 13 | switch(action.type) { 14 | case TodosActions.todoStateToggled: 15 | const newTodos = state.all.map(todo => { 16 | const newTodo = Object.assign({}, todo); 17 | if(newTodo.text === action.text) { 18 | newTodo.done = !newTodo.done; 19 | return newTodo; 20 | } 21 | return newTodo; 22 | }); 23 | return Object.assign({}, state, { 24 | all: newTodos 25 | }); 26 | case NewTodoActions.newTodoCreated: 27 | return Object.assign({}, state, { 28 | all: [ 29 | ...state.all, 30 | action.todo 31 | ] 32 | }); 33 | default: 34 | return state; 35 | } 36 | } -------------------------------------------------------------------------------- /13-styling-css/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /13-styling-css/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { TodoListContainer } from './TodoList'; 4 | import './index.css'; 5 | import { createStore, combineReducers, applyMiddleware } from 'redux'; 6 | import thunk from 'redux-thunk' 7 | import { Provider } from 'react-redux'; 8 | import { tabsReducer } from './TabsReducer'; 9 | import { newTodoReducer } from './NewTodoReducer'; 10 | import { todosReducer } from './TodosReducer'; 11 | 12 | const reducer = combineReducers({ 13 | todos: todosReducer, 14 | newTodo: newTodoReducer, 15 | tabs: tabsReducer 16 | }); 17 | const store = createStore(reducer, applyMiddleware(thunk)); 18 | 19 | ReactDOM.render( 20 | , 21 | document.getElementById('root') 22 | ); 23 | -------------------------------------------------------------------------------- /14-add-flow-types/.flowconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/14-add-flow-types/.flowconfig -------------------------------------------------------------------------------- /14-add-flow-types/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /14-add-flow-types/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "14-add-flow-types", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "redux-thunk": "^2.3.0", 7 | "react-redux": "^7.1.0", 8 | "redux": "^4.0.4", 9 | "react": "^16.8.6", 10 | "react-dom": "^16.8.6", 11 | "react-scripts": "3.0.1", 12 | "redux-logger": "^3.0.6" 13 | }, 14 | "devDependencies": { 15 | "chai": "^4.2.0", 16 | "chai-enzyme": "^1.0.0-beta.1", 17 | "enzyme": "^3.10.0", 18 | "enzyme-redux": "^0.2.1", 19 | "react-addons-test-utils": "^15.6.2", 20 | "flow-bin": "^0.102.0" 21 | }, 22 | "scripts": { 23 | "start": "react-scripts start", 24 | "build": "react-scripts build", 25 | "test": "react-scripts test --env=jsdom", 26 | "eject": "react-scripts eject", 27 | "flow": "flow" 28 | }, 29 | "browserslist": { 30 | "production": [ 31 | ">0.2%", 32 | "not dead", 33 | "not op_mini all" 34 | ], 35 | "development": [ 36 | "last 1 chrome version", 37 | "last 1 firefox version", 38 | "last 1 safari version" 39 | ] 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /14-add-flow-types/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/14-add-flow-types/public/favicon.ico -------------------------------------------------------------------------------- /14-add-flow-types/public/global.css: -------------------------------------------------------------------------------- 1 | body { 2 | max-width: 1024px; 3 | margin: 2em auto 2em auto; 4 | padding: 0 2em; 5 | font-family: sans-serif; 6 | } -------------------------------------------------------------------------------- /14-add-flow-types/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 17 | React App 18 | 19 | 20 |
    21 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /14-add-flow-types/src/AllTodos.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TodosActions from './TodosActions'; 5 | 6 | export class AllTodos extends React.Component { 7 | render() { 8 | return ( 9 |
      10 | {this.props.todos.map(this.renderTodo.bind(this))} 11 |
    12 | ); 13 | } 14 | 15 | renderTodo(todoData) { 16 | return ( 17 |
  • 18 | this.props.toggleTodoState(todoData.text) } /> 19 | {todoData.text} 20 |
  • ); 21 | } 22 | } 23 | 24 | function mapStateToProperties(state) { 25 | return { 26 | todos: state.todos.all 27 | }; 28 | } 29 | export const actionCreators = { 30 | toggleTodoState: (text) => { return { type: TodosActions.todoStateToggled, text: text }} 31 | } 32 | 33 | export const AllTodosContainer = connect(mapStateToProperties, actionCreators)(AllTodos); 34 | -------------------------------------------------------------------------------- /14-add-flow-types/src/NewTodo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import NewTodoActions from './NewTodoActions'; 5 | 6 | export class NewTodo extends React.Component { 7 | render() { 8 | return ( 9 |
    10 | 11 | this.props.newTodoSubmitted(this.props.newText)} /> 12 |
    13 | ); 14 | } 15 | } 16 | 17 | function createTodo(text) { 18 | return { text: text, done: false }; 19 | } 20 | 21 | function createTodoOnServer(text, callback) { 22 | //This function simulates the delay caused by creating a TODO on 23 | //a server. It just uses setTimeout instead of a real server communication. 24 | setTimeout(() => callback(createTodo(text)), 2000); 25 | } 26 | 27 | function createNewTodo(text) { 28 | return (dispatch) => { 29 | dispatch({type: NewTodoActions.creatingNewTodo }); 30 | createTodoOnServer(text, (todo) => dispatch({ type: NewTodoActions.newTodoCreated, todo: todo })) 31 | } 32 | } 33 | 34 | function mapStateToProperties(state) { 35 | return { 36 | newText: state.newTodo.text 37 | }; 38 | } 39 | export const actionCreators = { 40 | inputTextChanged: (event) => { return { type: NewTodoActions.newTodoTextChanged, text: event.target.value }}, 41 | newTodoSubmitted: (text) => createNewTodo(text) 42 | } 43 | 44 | export const NewTodoContainer = connect(mapStateToProperties, actionCreators)(NewTodo); 45 | -------------------------------------------------------------------------------- /14-add-flow-types/src/NewTodoActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | creatingNewTodo: Symbol("CREATING_NEW_TODO") 5 | }; 6 | -------------------------------------------------------------------------------- /14-add-flow-types/src/NewTodoReducer.js: -------------------------------------------------------------------------------- 1 | import NewTodoActions from './NewTodoActions'; 2 | 3 | const initialState = { text: "" }; 4 | 5 | export function newTodoReducer(state = initialState, action) { 6 | switch(action.type) { 7 | case NewTodoActions.creatingNewTodo: 8 | return Object.assign({}, state, { 9 | text: "" 10 | }); 11 | case NewTodoActions.newTodoTextChanged: 12 | return Object.assign({}, state, { 13 | text: action.text 14 | }); 15 | default: 16 | return state; 17 | } 18 | } -------------------------------------------------------------------------------- /14-add-flow-types/src/Overlay.css: -------------------------------------------------------------------------------- 1 | .overlay { 2 | background: rgba(0, 0, 0, 0.8); 3 | position: absolute; 4 | top: 0; bottom: 0; left: 0; right: 0; 5 | z-index: 1000 6 | } -------------------------------------------------------------------------------- /14-add-flow-types/src/Overlay.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import './Overlay.css'; 5 | 6 | export class Overlay extends React.Component { 7 | render() { 8 | const display = this.props.overlay? "block" : "none"; 9 | const style = { 10 | display: display 11 | }; 12 | return ( 13 |
    14 |
    15 | ); 16 | } 17 | } 18 | 19 | function mapStateToProperties(state) { 20 | return { 21 | overlay: state.tabs.overlay 22 | }; 23 | } 24 | 25 | export const OverlayContainer = connect(mapStateToProperties)(Overlay); 26 | -------------------------------------------------------------------------------- /14-add-flow-types/src/TabsActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /14-add-flow-types/src/TabsReducer.js: -------------------------------------------------------------------------------- 1 | import TabsActions from './TabsActions'; 2 | import NewTodoActions from './NewTodoActions'; 3 | 4 | const initialState = { activeTab: "all", overlay: false }; 5 | 6 | export function tabsReducer(state = initialState, action) { 7 | switch(action.type) { 8 | case TabsActions.tabSwitched: 9 | return Object.assign({}, state, { 10 | activeTab: action.tab 11 | }); 12 | case NewTodoActions.creatingNewTodo: 13 | return Object.assign({}, state, { 14 | overlay: true 15 | }); 16 | case NewTodoActions.newTodoCreated: 17 | return Object.assign({}, state, { 18 | activeTab: "all", 19 | overlay: false 20 | }); 21 | default: 22 | return state; 23 | } 24 | } -------------------------------------------------------------------------------- /14-add-flow-types/src/TodoList.css: -------------------------------------------------------------------------------- 1 | #tabs { 2 | list-style: none; 3 | margin: 0; 4 | padding: 0; 5 | } 6 | 7 | #tabs::after { 8 | content: " "; 9 | display: block; 10 | clear: both; 11 | } 12 | 13 | #tabs li { 14 | float: left; 15 | } 16 | 17 | #tabs li::after { 18 | content: "|"; 19 | margin-left: 1em; 20 | margin-right: 1em; 21 | } 22 | 23 | #tabs li:last-child::after { 24 | content: ""; 25 | margin: 0; 26 | } 27 | -------------------------------------------------------------------------------- /14-add-flow-types/src/TodoList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import TabsActions from './TabsActions'; 5 | import { AllTodosContainer } from './AllTodos'; 6 | import { NewTodoContainer } from './NewTodo'; 7 | import { OverlayContainer } from './Overlay'; 8 | 9 | import './TodoList.css'; 10 | 11 | export class TodoList extends Component { 12 | render() { 13 | const todosTab = ( 14 |
    15 |

    My Todos

    16 | 17 |
    18 | ); 19 | const newTodoTab = ( 20 |
    21 |

    New Todo

    22 | 23 |
    24 | ); 25 | const activeTab = this.props.activeTab==="all"? todosTab : newTodoTab; 26 | return ( 27 | 35 | ); 36 | } 37 | } 38 | 39 | function mapStateToProperties(state) { 40 | return { 41 | activeTab: state.tabs.activeTab 42 | }; 43 | } 44 | export const actionCreators = { 45 | switchTab: (tab) => { return { type: TabsActions.tabSwitched, tab: tab }} 46 | } 47 | 48 | export const TodoListContainer = connect(mapStateToProperties, actionCreators)(TodoList); 49 | -------------------------------------------------------------------------------- /14-add-flow-types/src/TodosActions.js: -------------------------------------------------------------------------------- 1 | export default { 2 | newTodoCreated: Symbol("NEW_TODO_CREATED"), 3 | newTodoTextChanged: Symbol("NEW_TODO_TEXT_CHANGED"), 4 | todoStateToggled: Symbol("TODO_STATE_TOGGLED"), 5 | tabSwitched: Symbol("TAB_SWITCHED") 6 | }; 7 | -------------------------------------------------------------------------------- /14-add-flow-types/src/TodosReducer.js: -------------------------------------------------------------------------------- 1 | import TodosActions from './TodosActions'; 2 | import NewTodoActions from './NewTodoActions'; 3 | 4 | const initialState = { 5 | all: [ 6 | { text: "Buy Milk", done: true }, 7 | { text: "Buy Beer", done: true }, 8 | { text: "Write workshop examples", done: false}, 9 | { text: "Host workshop", done: false } 10 | ] 11 | }; 12 | export function todosReducer(state = initialState, action) { 13 | switch(action.type) { 14 | case TodosActions.todoStateToggled: 15 | const newTodos = state.all.map(todo => { 16 | const newTodo = Object.assign({}, todo); 17 | if(newTodo.text === action.text) { 18 | newTodo.done = !newTodo.done; 19 | return newTodo; 20 | } 21 | return newTodo; 22 | }); 23 | return Object.assign({}, state, { 24 | all: newTodos 25 | }); 26 | case NewTodoActions.newTodoCreated: 27 | return Object.assign({}, state, { 28 | all: [ 29 | ...state.all, 30 | action.todo 31 | ] 32 | }); 33 | default: 34 | return state; 35 | } 36 | } -------------------------------------------------------------------------------- /14-add-flow-types/src/index.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/14-add-flow-types/src/index.css -------------------------------------------------------------------------------- /14-add-flow-types/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { TodoListContainer } from './TodoList'; 4 | import './index.css'; 5 | import { createStore, combineReducers, applyMiddleware } from 'redux'; 6 | import thunk from 'redux-thunk' 7 | import { Provider } from 'react-redux'; 8 | import { tabsReducer } from './TabsReducer'; 9 | import { newTodoReducer } from './NewTodoReducer'; 10 | import { todosReducer } from './TodosReducer'; 11 | 12 | const reducer = combineReducers({ 13 | todos: todosReducer, 14 | newTodo: newTodoReducer, 15 | tabs: tabsReducer 16 | }); 17 | const store = createStore(reducer, applyMiddleware(thunk)); 18 | 19 | ReactDOM.render( 20 | , 21 | document.getElementById('root') 22 | ); 23 | -------------------------------------------------------------------------------- /15-component-lifecycle/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-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 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | -------------------------------------------------------------------------------- /15-component-lifecycle/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "15-component-lifecycle", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "jointjs": "^3.0.2", 7 | "react-redux": "^7.1.0", 8 | "redux": "^4.0.4", 9 | "react": "^16.8.6", 10 | "react-dom": "^16.8.6", 11 | "react-scripts": "3.0.1" 12 | }, 13 | "scripts": { 14 | "start": "react-scripts start", 15 | "build": "react-scripts build", 16 | "test": "react-scripts test --env=jsdom", 17 | "eject": "react-scripts eject" 18 | }, 19 | "browserslist": { 20 | "production": [ 21 | ">0.2%", 22 | "not dead", 23 | "not op_mini all" 24 | ], 25 | "development": [ 26 | "last 1 chrome version", 27 | "last 1 firefox version", 28 | "last 1 safari version" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /15-component-lifecycle/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/15-component-lifecycle/public/favicon.ico -------------------------------------------------------------------------------- /15-component-lifecycle/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 18 | React App 19 | 20 | 21 |
    22 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /15-component-lifecycle/public/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | } 4 | 5 | body { 6 | margin: 0; 7 | padding: 0; 8 | } 9 | 10 | header { 11 | border-bottom: 1px solid black; 12 | } 13 | 14 | header ul { 15 | list-style: none; 16 | margin: 0; 17 | padding: 0; 18 | } 19 | 20 | header ul li { 21 | display: inline-block; 22 | margin: 0; 23 | padding: 0.5em 0; 24 | border-right: 1px solid black; 25 | } 26 | 27 | header ul li a { 28 | text-decoration: none; 29 | padding: 0.5em 1em; 30 | color: #555; 31 | } 32 | 33 | header ul li a:hover { 34 | background: #ddd; 35 | } 36 | 37 | header ul li a.active { 38 | background: #ddf; 39 | } 40 | 41 | svg { 42 | width: 200%; 43 | height: 100vh; 44 | } -------------------------------------------------------------------------------- /15-component-lifecycle/src/Header.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | export class Header extends React.PureComponent { 5 | render() { 6 | const renderedTabs = this.props.tabs.map((tab) => { 7 | const className = tab.active ? "active" : ""; 8 | return
  • this.props.onTabClicked(tab.id)}>{tab.name}
  • 9 | }); 10 | return ( 11 |
    12 |
      {renderedTabs}
    13 |
    14 | ); 15 | } 16 | } 17 | 18 | function mapStateToProperties(state) { 19 | return { 20 | tabs: state.tabs 21 | }; 22 | } 23 | const actionCreators = { 24 | onTabClicked: (id) => { return { type: 'ON_TAB_CLICKED', tabId: id}; } 25 | } 26 | 27 | export const HeaderContainer = connect(mapStateToProperties, actionCreators)(Header); -------------------------------------------------------------------------------- /15-component-lifecycle/src/MainSection.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | import { StationsViewContainer } from './stations/StationsView'; 5 | 6 | export class MainSection extends React.PureComponent { 7 | render() { 8 | if(this.props.activeTabId === 'network') { 9 | return ; 10 | } 11 | return
    Not yet implemented
    ; 12 | } 13 | } 14 | 15 | function mapStateToProperties(state) { 16 | return { 17 | activeTabId: state.tabs.find(tab => tab.active).id 18 | }; 19 | } 20 | 21 | export const MainSectionContainer = connect(mapStateToProperties)(MainSection); -------------------------------------------------------------------------------- /15-component-lifecycle/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { Provider } from 'react-redux' 4 | import { createStore, combineReducers } from 'redux' 5 | 6 | import { HeaderContainer } from './Header'; 7 | import { MainSectionContainer } from './MainSection'; 8 | 9 | import tabsReducer from './tabsReducer'; 10 | import stationsReducer from './stations/stationsReducer' 11 | 12 | const reducer = combineReducers({ 13 | tabs: tabsReducer, 14 | stations: stationsReducer 15 | }); 16 | let store = createStore(reducer); 17 | 18 | ReactDOM.render( 19 | 20 |
    21 | 22 | 23 |
    24 |
    , 25 | document.getElementById('root') 26 | ); 27 | -------------------------------------------------------------------------------- /15-component-lifecycle/src/stations/Link.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import joint from 'jointjs'; 3 | 4 | export class Link extends React.Component { 5 | render() { 6 | //This is how we create a link in JointJS, but where should I put it? 7 | const link = new joint.dia.Link({ 8 | source: { id: this.props.from.id }, 9 | target: { id: this.props.to.id } 10 | }); 11 | 12 | return
    FIXME Render link: {this.props.from.id} -> {this.props.to.id}
    ; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /15-component-lifecycle/src/stations/Station.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import joint from 'jointjs'; 3 | 4 | export class Station extends React.Component { 5 | render() { 6 | //Allright, this is how we render a JointJS rectangle, but where should I put it? 7 | const station = this.props.station; 8 | 9 | const rect = new joint.shapes.basic.Rect({ 10 | position: { x: station.x, y: station.y }, 11 | size: { width: 100, height: 30 }, 12 | attrs: { 13 | rect: { fill: 'blue' }, 14 | text: { text: station.name, fill: 'white' } 15 | } 16 | }); 17 | rect.set('id', station.id); 18 | 19 | rect.on('change:position', (event) => { 20 | this.props.onChangedPosition(station.id, event.attributes.position.x, event.attributes.position.y); 21 | }); 22 | 23 | return
    FIXME Render station: {station.name}
    ; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /15-component-lifecycle/src/stations/StationsView.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import joint from 'jointjs'; 4 | 5 | import { connect } from 'react-redux'; 6 | 7 | import { Station } from './Station' 8 | import { Link } from './Link' 9 | 10 | export class StationsView extends React.Component { 11 | constructor(props) { 12 | super(props); 13 | this.graph = new joint.dia.Graph(); 14 | this.cells=[]; 15 | } 16 | 17 | renderSubwayLine(lineData, lineId) { 18 | const links = lineData.map((v, i, l)=>{ return {current: v, next: l[i+1] }}).filter(linkData => linkData.next); 19 | const renderedLinks = links.map((linkData) => { 20 | return 21 | }); 22 | return renderedLinks; 23 | } 24 | 25 | renderStation(station) { 26 | return ; 27 | } 28 | 29 | render() { 30 | const allStations = this.props.stations.u1.concat(this.props.stations.u2); 31 | const renderedStations = allStations.map(this.renderStation.bind(this)); 32 | 33 | const renderedLinksByLine = [this.props.stations.u1, this.props.stations.u2].map(this.renderSubwayLine.bind(this)); 34 | const renderedLinks = renderedLinksByLine.reduce((prev, cur) => { return prev.concat(cur)}); 35 | 36 | //Hmm... Somehow this does not work. How do we get our components into the cells? 37 | const paper = new joint.dia.Paper({ 38 | el: ReactDOM.findDOMNode(this.refs.placeholder), 39 | width: 600, 40 | height: 200, 41 | model: this.graph, 42 | gridSize: 1 43 | }); 44 | 45 | this.graph.addCells(this.cells); 46 | 47 | return
    {renderedStations.concat(renderedLinks)}
    ; 48 | } 49 | } 50 | 51 | function mapStateToProperties(state) { 52 | return { 53 | stations: state.stations 54 | }; 55 | } 56 | const actionCreators = { 57 | onChangedPosition: (id, x, y) => { 58 | return { 59 | type: 'STATION_MOVED', 60 | id: id, 61 | x: x, 62 | y: y 63 | }; 64 | } 65 | } 66 | 67 | export const StationsViewContainer = connect(mapStateToProperties, actionCreators)(StationsView); 68 | -------------------------------------------------------------------------------- /15-component-lifecycle/src/stations/stationsReducer.js: -------------------------------------------------------------------------------- 1 | const initialState = { 2 | u1: [ 3 | { name: "Steyregg", id: "steyr", x: 1050, y: 250}, 4 | { name: "VOEST", id: "voest", x: 950, y: 250}, 5 | { name: "Bulgariplatz", id: "bulg", x: 850, y: 250}, 6 | { name: "Hauptbahnhof", id: "hbf", x: 750, y: 250}, 7 | { name: "Stadion", id: "stad", x: 650, y: 250}, 8 | { name: "Gaumberg", id: "gaumb", x: 550, y: 250}, 9 | { name: "Haag", id: "haag", x: 450, y: 250}, 10 | { name: "Doppl", id: "doppl", x: 350, y: 250}, 11 | { name: "Plus City", id: "plus", x: 250, y: 250}, 12 | { name: "Flughafen", id: "airp", x: 150, y: 250} 13 | ], 14 | u2: [ 15 | { name: "Universitaet", id: "uni", x: 750, y: 50 }, 16 | { name: "St. Magdalena - Kirche", id: "smk", x: 750, y: 100 }, 17 | { name: "Urfahr", id: "urf", x: 750, y: 150 }, 18 | { name: "Wildbergstr.", id: "wbs", x: 750, y: 200 }, 19 | { name: "Hauptbahnhof", id: "hbf", x: 750, y: 250}, 20 | { name: "Bindermichl", id: "bin", x: 750, y: 300 }, 21 | { name: "Kleinm.", id: "km", x: 750, y: 350 }, 22 | { name: "Ennsfeld", id: "ennsf", x: 750, y: 400 }, 23 | { name: "Ebelsberg", id: "ebel", x: 750, y: 450 }, 24 | { name: "Solar City", id: "sol", x: 750, y: 500} 25 | ] 26 | }; 27 | export default function(state = initialState, action) { 28 | switch(action.type) { 29 | case 'STATION_MOVED': 30 | return Object.assign({}, state, { 31 | 'u1': _updateStationPositionInLine(action), 32 | 'u2': _updateStationPositionInLine(action)}); 33 | default: 34 | return state; 35 | } 36 | } 37 | 38 | function _updateStationPositionInLine(action) { 39 | return line => line.map(station => { 40 | if(station.id === action.id) { 41 | return Object.assign({}, station, {x: action.x, y: action.y}); 42 | } 43 | return station; 44 | }) 45 | } -------------------------------------------------------------------------------- /15-component-lifecycle/src/tabsReducer.js: -------------------------------------------------------------------------------- 1 | const initialState = [ 2 | { name: "Network", id: "network", active: true }, 3 | { name: "Plan Journey", id: "plan", active: false } 4 | ]; 5 | export default function(state=initialState, action) { 6 | switch(action.type) { 7 | case 'ON_TAB_CLICKED': 8 | return state.map((tab) => { 9 | return Object.assign({}, tab, { active: tab.id === action.tabId}); 10 | }); 11 | default: 12 | return state; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/.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 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/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 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "20-todo-list-from-scratch", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.8.6", 7 | "react-dom": "^16.8.6", 8 | "react-scripts": "3.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test", 14 | "eject": "react-scripts eject" 15 | }, 16 | "eslintConfig": { 17 | "extends": "react-app" 18 | }, 19 | "browserslist": { 20 | "production": [ 21 | ">0.2%", 22 | "not dead", 23 | "not op_mini all" 24 | ], 25 | "development": [ 26 | "last 1 chrome version", 27 | "last 1 firefox version", 28 | "last 1 safari version" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dtanzer/react-basic-examples/881e287ea5e0fb81931d8101a78109541582fd60/20-todo-list-from-scratch/public/favicon.ico -------------------------------------------------------------------------------- /20-todo-list-from-scratch/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
    27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/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 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | function App() { 6 | return ( 7 |
    8 |
    9 | logo 10 |

    11 | Edit src/App.js and save to reload. 12 |

    13 | 19 | Learn React 20 | 21 |
    22 |
    23 | ); 24 | } 25 | 26 | export default App; 27 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/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 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/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(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /20-todo-list-from-scratch/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.1/8 is 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 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 David Tanzer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-basic-examples 2 | Examples for my "React / Redux Basic Workshop" 3 | --------------------------------------------------------------------------------