RTK

113 阅读1分钟

1 添加依赖@reduxjs/toolkit,react-redux

image.png

image.png

2 全局组件app 添加store.要引入store import store from './store/store'

image.png

image.png

3 创建store ,引入reducer

image.png

4 slice.js reducer写在这里面

image.png

image.png

5

image.png

image.png

6 add是方法,action确定哪个切片下的哪个方法,state.n是所在切片初始值的key值表示

import { useDispatch, useSelector } from 'react-redux' //count是countSlice切片里的name对应的count代表使用的是countSlice切片 const { count,emp } = useSelector(state => state) image.png

image.png

7 state.ename = action.payload 中action.payload 指的是 dispatch(setName('小明'))的小明

image.png

image.png

image.png

image.png