66 | );
67 | }
68 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------