Skip to content

Commit 978c972

Browse files
authored
Create redux toolkit README.md
1 parent 9c36cff commit 978c972

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

redux-toolkit/README.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Redux Toolkit
2+
Redux Toolkit is a library that provides utilities and abstractions to simplify the development of Redux applications. It includes features like simplified configuration, standardized Redux patterns, and built-in support for common Redux use cases.
3+
4+
By using Redux Toolkit, developers can write Redux code more efficiently and with fewer boilerplate, making it easier to manage state in complex applications.
5+
6+
Here's an example of using Redux Toolkit:
7+
8+
1. Install Redux Toolkit:
9+
```
10+
npm install @reduxjs/toolkit
11+
```
12+
13+
2. Create a Redux store using `configureStore` from Redux Toolkit:
14+
```javascript
15+
import { configureStore } from '@reduxjs/toolkit';
16+
import counterReducer from './counterSlice';
17+
18+
const store = configureStore({
19+
reducer: {
20+
counter: counterReducer,
21+
},
22+
});
23+
24+
export default store;
25+
```
26+
27+
3. Create a slice using `createSlice` from Redux Toolkit:
28+
```javascript
29+
import { createSlice } from '@reduxjs/toolkit';
30+
31+
const counterSlice = createSlice({
32+
name: 'counter',
33+
initialState: 0,
34+
reducers: {
35+
increment: (state) => state + 1,
36+
decrement: (state) => state - 1,
37+
},
38+
});
39+
40+
export const { increment, decrement } = counterSlice.actions;
41+
export default counterSlice.reducer;
42+
```
43+
44+
4. Use the Redux store and actions in your components:
45+
```javascript
46+
import React from 'react';
47+
import { useDispatch, useSelector } from 'react-redux';
48+
import { increment, decrement } from './counterSlice';
49+
50+
const Counter = () => {
51+
const count = useSelector((state) => state.counter);
52+
const dispatch = useDispatch();
53+
54+
const handleIncrement = () => {
55+
dispatch(increment());
56+
};
57+
58+
const handleDecrement = () => {
59+
dispatch(decrement());
60+
};
61+
62+
return (
63+
<div>
64+
<p>Count: {count}</p>
65+
<button onClick={handleIncrement}>Increment</button>
66+
<button onClick={handleDecrement}>Decrement</button>
67+
</div>
68+
);
69+
};
70+
71+
export default Counter;
72+
```
73+
74+
In this example, Redux Toolkit simplifies the process of setting up the Redux store and defining reducers. The `configureStore` function sets up the store with the `counterReducer` defined using `createSlice`. The `createSlice` function generates action creators and a reducer based on the provided configuration. The `useSelector` hook allows components to access the Redux state, and the `useDispatch` hook provides a way to dispatch actions.
75+
76+
<br>
77+
78+
## APIs
79+
1. `configureStore`: This API is used to create a Redux store with sensible defaults and configurations. It combines several Redux store setup steps into a single function call. You can provide a reducer object, middleware, and other options to customize the store setup.
80+
81+
2. `createSlice`: This API generates action creators and a reducer based on a provided configuration object. It helps in writing concise and predictable Redux code by reducing the amount of boilerplate code required. The configuration object includes the slice name, initial state, and an object with reducer functions for different actions.
82+
83+
3. `createAsyncThunk`: This API simplifies handling asynchronous actions in Redux. It generates an action creator that dispatches multiple actions during the different stages of an asynchronous operation (e.g., pending, fulfilled, rejected). It can be used with APIs like `axios` or `fetch` to handle data fetching and other async operations.
84+
85+
4. `createEntityAdapter`: This API is used to simplify the management of normalized data in the Redux store. It provides utility functions to work with entities and collections, such as adding, updating, and removing entities. It also generates selectors for querying and manipulating the normalized data.
86+
87+
5. `createReducer`: This API allows you to define a reducer function using a "builder callback" syntax. It provides a simpler and more readable way to handle complex reducer logic, including conditional logic, immutability, and handling different action types.
88+
89+
6. `createSelector`: This API is used to create memoized selectors for efficiently computing derived data from the Redux store. Memoized selectors cache the results and only recalculate when the input state changes. It helps in optimizing the performance of your application by avoiding unnecessary computations.
90+
91+
7. `createAction`: This API generates a simple action creator function that returns an action object with a specified type and payload. It helps in creating concise and readable action creators without writing the entire action object manually. The API takes a string parameter representing the action type and an optional payload creator function.
92+
93+
<br>
94+
95+
## Reference
96+
https://redux-toolkit.js.org/introduction/getting-started

0 commit comments

Comments
 (0)