-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduxt biolerplate.txt
59 lines (31 loc) · 996 Bytes
/
reduxt biolerplate.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
How to create slices:
import { createSlice } from "@reduxjs/toolkit";
const userSlice = createSlice({
name : 'allUsers',
initialState : [],
reducers : {
adduser(){},
},
})
export const {adduser} = userSlice.actions;
export default userSlice.reducer;
configure store:
import { configureStore } from "@reduxjs/toolkit";
import userSlice from "./slices/userSlice";
const store = configureStore ({
reducer : userSlice,
})
export default store;
Connect redux with react
import { Provider } from 'react-redux';
import store from './store/store';
<Provider store={store}></Provider>
how to get data from redux toolkit in any component:
import { useSelector } from "react-redux";
const data = useSelector((state) => state.user);
How to pass data to redux toolkit:
import { useDispatch } from "react-redux";
const dispatch = useDispatch()
first call a function like onClick
and then use
dispatch (addUser()) function that we created in slice reducers like addUsers(){}