How to create a React + Redux App
井民全, Jing, mqjing@gmail.com
GitHub: Source
Why Redux?
Redux is a library for managing App state. React has in-build hooks such as useState, ...etc. to do the same thing. However, the Redux way provides a central way to control the app state. Check the following statement from the official doc.
This document is just my note for the React + Redux template that includes
How to create a React + Redux template app?
How to create a store for the app? (in store.ts)
How to define the component state update logic (in xxxSlices.ts)
How a component extracts the state from the store (using selector hook)
How a component sends activity to the corresponding reducer (using dispatch)
How a component mixed the React in-build state function (useState) and the Redux useSelect? (in Counter.tsx)
However, you should read the tutorial from the Redux official site.
Ok, let's show the instruction and the code.
By Jing.
Table of contents
1. Create React + Redux app template 3
2.1. Step 1: Define the App Store 3
2.2. Step 2: Define the component Redux logic 4
2.3. Step 3: Define the React Component 6
1. Create React + Redux app template
2. Code
2.1. Step 1: Define the App Store
File: src/app/store.ts
2.2. Step 2: Define the component Redux logic
File: src/features/counter/counterSlice.ts
2.3. Step 3: Define the React Component
File: /src/features/counter/Counter.tsx
2.4. App Main
File: src/App.tsx
3. Install & Run
4. Clean
File: ./package.json
yarn clean
5. References
Getting Start with Redux, https://redux.js.org/introduction/getting-started
Redux Essentials, Part 2: Redux App Structure, https://redux.js.org/tutorials/essentials/part-2-app-structure
Redux Toolkit TypeScript Quick Start, https://redux-toolkit.js.org/tutorials/typescript
createAsyncThunk, https://redux-toolkit.js.org/api/createAsyncThunk
