• Reactjs数据篇


    参考代码:GitHub - leellun/zhiqu-web-test2

    1 通过createAction创建Action做数据传递

    Redux 中定义动作的常用方法是分别声明一个动作类型常量和一个动作创建器函数来构造该类型的动作。

    store/action.ts

    1. import { createAction } from "@reduxjs/toolkit";
    2. const changeChannelAction2 = createAction('changeChannelAction2', (text: string)=> {
    3. return {
    4. payload: {
    5. name:text
    6. } as any,
    7. }
    8. })
    9. export { changeChannelAction2 };

    通过createSlice创建切片状态:store/slice.ts

    通过builder.addCase("changeChannelAction2",fn)接收type为changeChannelAction2的数据

    1. import { createSlice } from "@reduxjs/toolkit";
    2. const channelSlice = createSlice({
    3. name:"channel",
    4. initialState:{
    5. channel:{
    6. name:'3345345'
    7. }
    8. },
    9. reducers:{
    10. },
    11. extraReducers:(builder)=>{
    12. builder.addCase("changeChannelAction2",(state,action:any)=>{
    13. console.log("=234=234")
    14. state.channel.name=action.payload.name
    15. })
    16. }
    17. })
    18. const { changeChannel } = channelSlice.actions;
    19. const channelReducer = channelSlice.reducer;
    20. export {changeChannel,channelReducer}

     然后通过dispatch分发数据

     store.dispatch(changeChannelAction2("sdf"))

    控制台将会打印

    =234=234

    2 通过createAsyncThunk异步加载数据,并且传递数据

    1. import { createAsyncThunk } from "@reduxjs/toolkit";
    2. const changeChannelAction = createAsyncThunk(
    3. 'changeChannelAction',
    4. async (extraInfo: { userId: string }, { dispatch }) => {
    5. const { userId } = extraInfo;
    6. return userId+"====createAsyncThunk="
    7. }
    8. );
    9. export { changeChannelAction };

    3  通过createReducer处理数据和状态化数据

    1. import { createReducer } from "@reduxjs/toolkit";
    2. const userReducer = createReducer(
    3. {
    4. id:'',
    5. name:"sdfsdf"
    6. },
    7. (builder) => {
    8. builder
    9. .addCase("changeChannelAction2", (state, action:any) => {
    10. console.log(action.payload.name)
    11. state.name = action.payload.name;
    12. })
    13. }
    14. );
    15. export default userReducer;

    4 多type数据处理状态化

    1. const moreReducer = (state:any, action: any) => {
    2. state=action.payload
    3. console.log("moreReducer",state)
    4. if(state===undefined){
    5. return {}
    6. }
    7. switch (action.type) {
    8. case 'changeChannelAction':
    9. return state;
    10. case 'changeChannelAction2':
    11. return state;
    12. default:
    13. return state;
    14. }
    15. };
    16. export default moreReducer;

     5 同步方法指定类型和载体

    1. export function changeUserMsg(payload: any) {
    2. return {
    3. type: 'changeChannelAction',
    4. payload
    5. };
    6. }
    7. store.dispatch(changeUserMsg({name:''}))

     6 异步方法指定类型和载体

    1. export function changeAsyncUserMsg(payload: any) {
    2. return async (dispatch:any)=>{
    3. dispatch(changeUserMsg(payload))
    4. };
    5. }

    7 获取store中的数据

    1. const CounterComponent = () => {
    2. const name = useSelector((state:any) => {
    3. return state.channelReducer.channel.name
    4. })
    5. return <div>{name}</div>
    6. }

    8 将store和组件结合

    1. <Provider store={store}>
    2. </Provider>

     可以在Provider作用访问类使用useDispatcher()

    完整代码:

    store/action.ts

    1. import { createAction } from "@reduxjs/toolkit";
    2. const changeChannelAction2 = createAction('changeChannelAction2', (text: string)=> {
    3. return {
    4. payload: {
    5. id:'',
    6. name:text
    7. } as any,
    8. }
    9. })
    10. export { changeChannelAction2 };

    store/reducer.ts

    1. import { createReducer } from "@reduxjs/toolkit";
    2. const userReducer = createReducer(
    3. {
    4. id:'',
    5. name:"sdfsdf"
    6. },
    7. (builder) => {
    8. builder
    9. .addCase("changeChannelAction2", (state, action:any) => {
    10. console.log(action.payload.name)
    11. state.name = action.payload.name;
    12. })
    13. }
    14. );
    15. export default userReducer;

    store/thunk.ts

    1. import { createAsyncThunk } from "@reduxjs/toolkit";
    2. const changeChannelAction = createAsyncThunk(
    3. 'changeChannelAction',
    4. async (extraInfo: { userId: string }, { dispatch }) => {
    5. const { userId } = extraInfo;
    6. return userId+"====createAsyncThunk="
    7. }
    8. );
    9. export { changeChannelAction };

    store/slice.ts 

    1. import { createSlice } from "@reduxjs/toolkit";
    2. import { changeChannelAction } from "./thunk";
    3. const channelSlice = createSlice({
    4. name:"channel",
    5. initialState:{
    6. channel:{
    7. id:'',
    8. name:'3345345'
    9. }
    10. },
    11. reducers:{
    12. changeChannel(state, { payload }) {
    13. console.log(payload)
    14. state.channel = payload;
    15. }
    16. },
    17. extraReducers:(builder)=>{
    18. builder.addCase(changeChannelAction.pending, (state, action:any) => {
    19. console.log(action.payload,"===1")
    20. })
    21. builder.addCase(changeChannelAction.fulfilled, (state, action:any) => {console.log(action.payload,"===2")})
    22. builder.addCase(changeChannelAction.rejected, (state, action:any) => {console.log(action.payload,"===3")})
    23. builder.addCase("changeChannelAction2",(state,action:any)=>{
    24. console.log("=234=234")
    25. state.channel.name=action.payload.name
    26. })
    27. }
    28. })
    29. const { changeChannel } = channelSlice.actions;
    30. const channelReducer = channelSlice.reducer;
    31. export {changeChannel,channelReducer}

    store/moreReducer.ts

    1. const moreReducer = (state:any, action: any) => {
    2. state=action.payload
    3. console.log("moreReducer",state)
    4. if(state===undefined){
    5. return {}
    6. }
    7. switch (action.type) {
    8. case 'changeChannelAction':
    9. return state;
    10. case 'changeChannelAction2':
    11. return state;
    12. default:
    13. return state;
    14. }
    15. };
    16. export default moreReducer;

     store/method.ts

    1. export function changeUserMsg(payload: any) {
    2. return {
    3. type: 'changeChannelAction',
    4. payload
    5. };
    6. }
    7. export function changeAsyncUserMsg(payload: any) {
    8. return async (dispatch:any)=>{
    9. dispatch(changeUserMsg(payload))
    10. };
    11. }

     store/index.ts

    1. import { configureStore } from "@reduxjs/toolkit";
    2. import {changeChannel, channelReducer} from './slice'
    3. import userReducer from './reducer'
    4. import moreReducer from './moreReducer'
    5. const store = configureStore({
    6. reducer: {
    7. channelReducer,
    8. userReducer,
    9. moreReducer
    10. },
    11. devTools: true
    12. });
    13. export default store;

     app.tsx

    1. import React, { useCallback, useState } from 'react';
    2. import logo from './logo.svg';
    3. import './App.css';
    4. import { Provider, useSelector } from 'react-redux';
    5. import store from './store';
    6. import { changeChannelAction2 } from './store/action';
    7. import { changeChannelAction } from './store/thunk';
    8. import { changeChannel } from './store/slice';
    9. import { changeAsyncUserMsg, changeUserMsg } from './store/method';
    10. function App() {
    11. const [count, setCount] = useState(0);
    12. const handleClick = useCallback(() => {
    13. console.log(`Clicked ${count} times`);
    14. }, [count]);
    15. store.dispatch(changeChannelAction({userId:"234234243"}))
    16. store.dispatch(changeChannelAction2("sdf"))
    17. store.dispatch(changeChannel({id:"23",name:"test3"}))
    18. store.dispatch(changeUserMsg("aaaaaaaaa"))
    19. store.dispatch(changeAsyncUserMsg("bbbbbbbb"))
    20. const CounterComponent = () => {
    21. const name = useSelector((state:any) => {
    22. return state.channelReducer.channel.name
    23. })
    24. return <div>{name}</div>
    25. }
    26. return (
    27. <div className="App">
    28. <Provider store={store}>
    29. <header className="App-header">
    30. <img src={logo} className="App-logo" alt="logo" />
    31. <p>
    32. Edit <code>src/App.tsx</code> and save to reload.
    33. </p>
    34. <a
    35. className="App-link"
    36. href="https://reactjs.org"
    37. target="_blank"
    38. rel="noopener noreferrer"
    39. >
    40. Learn React
    41. </a>
    42. </header>
    43. <div>Count: {count}</div>
    44. <button onClick={() => setCount(count + 1)}>Increment</button>
    45. <button onClick={handleClick}>Click me</button>
    46. <CounterComponent/>
    47. </Provider>
    48. </div>
    49. );
    50. }
    51. export default App;

    推荐文章:

    React进阶系列之数据流 - 掘金 (juejin.cn)

    滑动验证页面

  • 相关阅读:
    Ubuntu20.04 PostgreSQL 14 安装配置记录
    mysql 查询表 所有字段
    支持控件drag和click
    Python基础:【习题系列】多选题(一)
    Qt5开发从入门到精通——终章、数据库基本概念(后续篇章升级为 QT常规应用开发)
    excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
    上周热点回顾(3.25-3.31)
    删除linux(centos7)系统自带的open jdk,安装配置jdk环境
    【故障补牢】贪吃的 Bing 爬虫,限量供应的应对措施
    【转载】分布式训练和集合通信
  • 原文地址:https://blog.csdn.net/liu289747235/article/details/138099297