├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── app └── store.js ├── components └── counter │ ├── Counter.js │ ├── Counter.module.css │ ├── counterAPI.js │ ├── counterSlice.js │ └── counterSlice.spec.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js ├── routes ├── expenses.jsx └── invoices.jsx └── setupTests.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App and Redux 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app), using the [Redux](https://redux.js.org/) and [Redux Toolkit](https://redux-toolkit.js.org/) template. 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser. 13 | 14 | The page will reload when you make changes.\ 15 | You may also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 35 | 36 | 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. 37 | 38 | 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. 39 | 40 | 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. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@reduxjs/toolkit": "^1.8.2", 7 | "@testing-library/jest-dom": "^5.16.4", 8 | "@testing-library/react": "^13.2.0", 9 | "@testing-library/user-event": "^14.2.0", 10 | "antd": "^4.20.6", 11 | "react": "^18.1.0", 12 | "react-dom": "^18.1.0", 13 | "react-redux": "^8.0.2", 14 | "react-router-dom": "^6.3.0", 15 | "react-scripts": "5.0.1", 16 | "web-vitals": "^2.1.4" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": [ 26 | "react-app", 27 | "react-app/jest" 28 | ] 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxxsf/react-redux-tutorial/48123dd03b558a7d1bba7eec2750e57972332c60/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React Redux App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxxsf/react-redux-tutorial/48123dd03b558a7d1bba7eec2750e57972332c60/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xxxsf/react-redux-tutorial/48123dd03b558a7d1bba7eec2750e57972332c60/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | .App-header { 11 | min-height: 100vh; 12 | display: flex; 13 | flex-direction: column; 14 | align-items: center; 15 | justify-content: center; 16 | font-size: calc(10px + 2vmin); 17 | } 18 | 19 | .App-link { 20 | color: rgb(112, 76, 182); 21 | } 22 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { 3 | Routes, 4 | Route, 5 | } from "react-router-dom"; 6 | import { Link } from "react-router-dom"; 7 | 8 | import Expenses from "./routes/expenses"; 9 | import Invoices from "./routes/invoices"; 10 | 11 | 12 | import './App.css'; 13 | 14 | function App() { 15 | return ( 16 |
17 |
18 | 19 | 27 | 28 | 29 | {/* } /> */} 30 | } /> 31 | } /> 32 | } /> 33 | 34 |
35 | 36 | 37 |
38 | ); 39 | } 40 | 41 | export default App; 42 | -------------------------------------------------------------------------------- /src/app/store.js: -------------------------------------------------------------------------------- 1 | import { configureStore } from '@reduxjs/toolkit'; 2 | import counterReducer from '../components/counter/counterSlice'; 3 | 4 | export const store = configureStore({ 5 | reducer: { 6 | counter: counterReducer, 7 | }, 8 | }); 9 | -------------------------------------------------------------------------------- /src/components/counter/Counter.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { useSelector, useDispatch } from 'react-redux'; 3 | import { 4 | decrement, 5 | increment, 6 | incrementByAmount, 7 | incrementAsync, 8 | incrementIfOdd, 9 | selectCount, 10 | } from './counterSlice'; 11 | import styles from './Counter.module.css'; 12 | 13 | export function Counter() { 14 | const count = useSelector(selectCount); 15 | const dispatch = useDispatch(); 16 | const [incrementAmount, setIncrementAmount] = useState('2'); 17 | 18 | const incrementValue = Number(incrementAmount) || 0; 19 | 20 | return ( 21 |
22 |
23 | 30 | {count} 31 | 38 |
39 |
40 | setIncrementAmount(e.target.value)} 45 | /> 46 | 52 | 58 | 64 |
65 |
66 | ); 67 | } 68 | -------------------------------------------------------------------------------- /src/components/counter/Counter.module.css: -------------------------------------------------------------------------------- 1 | .row { 2 | display: flex; 3 | align-items: center; 4 | justify-content: center; 5 | } 6 | 7 | .row > button { 8 | margin-left: 4px; 9 | margin-right: 8px; 10 | } 11 | .row:not(:last-child) { 12 | margin-bottom: 16px; 13 | } 14 | 15 | .value { 16 | font-size: 78px; 17 | padding-left: 16px; 18 | padding-right: 16px; 19 | margin-top: 2px; 20 | font-family: 'Courier New', Courier, monospace; 21 | } 22 | 23 | .button { 24 | appearance: none; 25 | background: none; 26 | font-size: 32px; 27 | padding-left: 12px; 28 | padding-right: 12px; 29 | outline: none; 30 | border: 2px solid transparent; 31 | color: rgb(112, 76, 182); 32 | padding-bottom: 4px; 33 | cursor: pointer; 34 | background-color: rgba(112, 76, 182, 0.1); 35 | border-radius: 2px; 36 | transition: all 0.15s; 37 | } 38 | 39 | .textbox { 40 | font-size: 32px; 41 | padding: 2px; 42 | width: 64px; 43 | text-align: center; 44 | margin-right: 4px; 45 | } 46 | 47 | .button:hover, 48 | .button:focus { 49 | border: 2px solid rgba(112, 76, 182, 0.4); 50 | } 51 | 52 | .button:active { 53 | background-color: rgba(112, 76, 182, 0.2); 54 | } 55 | 56 | .asyncButton { 57 | composes: button; 58 | position: relative; 59 | } 60 | 61 | .asyncButton:after { 62 | content: ''; 63 | background-color: rgba(112, 76, 182, 0.15); 64 | display: block; 65 | position: absolute; 66 | width: 100%; 67 | height: 100%; 68 | left: 0; 69 | top: 0; 70 | opacity: 0; 71 | transition: width 1s linear, opacity 0.5s ease 1s; 72 | } 73 | 74 | .asyncButton:active:after { 75 | width: 0%; 76 | opacity: 1; 77 | transition: 0s; 78 | } 79 | -------------------------------------------------------------------------------- /src/components/counter/counterAPI.js: -------------------------------------------------------------------------------- 1 | // A mock function to mimic making an async request for data 2 | export function fetchCount(amount = 1) { 3 | return new Promise((resolve) => 4 | setTimeout(() => resolve({ data: amount }), 500) 5 | ); 6 | } 7 | -------------------------------------------------------------------------------- /src/components/counter/counterSlice.js: -------------------------------------------------------------------------------- 1 | import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; 2 | import { fetchCount } from './counterAPI'; 3 | 4 | const initialState = { 5 | value: 0, 6 | status: 'idle', 7 | }; 8 | 9 | // The function below is called a thunk and allows us to perform async logic. It 10 | // can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This 11 | // will call the thunk with the `dispatch` function as the first argument. Async 12 | // code can then be executed and other actions can be dispatched. Thunks are 13 | // typically used to make async requests. 14 | export const incrementAsync = createAsyncThunk( 15 | 'counter/fetchCount', 16 | async (amount) => { 17 | const response = await fetchCount(amount); 18 | // The value we return becomes the `fulfilled` action payload 19 | return response.data; 20 | } 21 | ); 22 | 23 | export const counterSlice = createSlice({ 24 | name: 'counter', 25 | initialState, 26 | // The `reducers` field lets us define reducers and generate associated actions 27 | reducers: { 28 | increment: (state) => { 29 | // Redux Toolkit allows us to write "mutating" logic in reducers. It 30 | // doesn't actually mutate the state because it uses the Immer library, 31 | // which detects changes to a "draft state" and produces a brand new 32 | // immutable state based off those changes 33 | state.value += 1; 34 | }, 35 | decrement: (state) => { 36 | state.value -= 1; 37 | }, 38 | // Use the PayloadAction type to declare the contents of `action.payload` 39 | incrementByAmount: (state, action) => { 40 | state.value += action.payload; 41 | }, 42 | }, 43 | // The `extraReducers` field lets the slice handle actions defined elsewhere, 44 | // including actions generated by createAsyncThunk or in other slices. 45 | extraReducers: (builder) => { 46 | builder 47 | .addCase(incrementAsync.pending, (state) => { 48 | state.status = 'loading'; 49 | }) 50 | .addCase(incrementAsync.fulfilled, (state, action) => { 51 | state.status = 'idle'; 52 | state.value += action.payload; 53 | }); 54 | }, 55 | }); 56 | 57 | export const { increment, decrement, incrementByAmount } = counterSlice.actions; 58 | 59 | // The function below is called a selector and allows us to select a value from 60 | // the state. Selectors can also be defined inline where they're used instead of 61 | // in the slice file. For example: `useSelector((state: RootState) => state.counter.value)` 62 | export const selectCount = (state) => state.counter.value; 63 | 64 | // We can also write thunks by hand, which may contain both sync and async logic. 65 | // Here's an example of conditionally dispatching actions based on current state. 66 | export const incrementIfOdd = (amount) => (dispatch, getState) => { 67 | const currentValue = selectCount(getState()); 68 | if (currentValue % 2 === 1) { 69 | dispatch(incrementByAmount(amount)); 70 | } 71 | }; 72 | 73 | export default counterSlice.reducer; 74 | -------------------------------------------------------------------------------- /src/components/counter/counterSlice.spec.js: -------------------------------------------------------------------------------- 1 | import counterReducer, { 2 | increment, 3 | decrement, 4 | incrementByAmount, 5 | } from './counterSlice'; 6 | 7 | describe('counter reducer', () => { 8 | const initialState = { 9 | value: 3, 10 | status: 'idle', 11 | }; 12 | it('should handle initial state', () => { 13 | expect(counterReducer(undefined, { type: 'unknown' })).toEqual({ 14 | value: 0, 15 | status: 'idle', 16 | }); 17 | }); 18 | 19 | it('should handle increment', () => { 20 | const actual = counterReducer(initialState, increment()); 21 | expect(actual.value).toEqual(4); 22 | }); 23 | 24 | it('should handle decrement', () => { 25 | const actual = counterReducer(initialState, decrement()); 26 | expect(actual.value).toEqual(2); 27 | }); 28 | 29 | it('should handle incrementByAmount', () => { 30 | const actual = counterReducer(initialState, incrementByAmount(2)); 31 | expect(actual.value).toEqual(5); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | } 4 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { createRoot } from 'react-dom/client'; 3 | import { Provider } from 'react-redux'; 4 | import { store } from './app/store'; 5 | import App from './App'; 6 | import reportWebVitals from './reportWebVitals'; 7 | import { 8 | HashRouter, 9 | } from "react-router-dom"; 10 | 11 | import './index.css'; 12 | 13 | const container = document.getElementById('root'); 14 | const root = createRoot(container); 15 | 16 | root.render( 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | ); 25 | 26 | // If you want to start measuring performance in your app, pass a function 27 | // to log results (for example: reportWebVitals(console.log)) 28 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 29 | reportWebVitals(); 30 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/routes/expenses.jsx: -------------------------------------------------------------------------------- 1 | export default function Expenses() { 2 | return ( 3 |
4 |

Expenses

5 |
6 | ); 7 | } -------------------------------------------------------------------------------- /src/routes/invoices.jsx: -------------------------------------------------------------------------------- 1 | import { Counter } from '../components/counter/Counter'; 2 | 3 | export default function Invoices() { 4 | return ( 5 |
6 |

Invoices

7 | 8 |

9 | Edit src/App.js and save to reload. 10 |

11 |
12 | ); 13 | } -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | --------------------------------------------------------------------------------