redux學(xué)習(xí)筆記之createSlice、createAsyncThunk

npm install redux-toolkit

1.數(shù)據(jù)流


afe24fa579e2470ba9465e852c17d090.png

上代碼
2.Home/homeSlice.js

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import * as service from '../service'; // API

// 網(wǎng)絡(luò)請求
export const getBasicInfo = createAsyncThunk(
  'home/getBasicInfo',
  (params, thunkAPI) => {
    console.log('dispatchParams', params);
    return  service.getBasicInfo();
  }
);

const homeSlice = createSlice({
  name: 'home',
  initialState: {
    value: 0,
    loading: 'idle',
    userPoints: {},
    quickEntrance: [],
    banner: [],
  },
  reducers: {
    increment(state) {
      state.value++;
    },
    decrement(state) {
      state.value--;
    },
    setValue(state, {payload}) {
    state.value = payload
  },
  extraReducers: builder => {
    builder.addCase(getBasicInfo.pending, (state, action) => {
      state.loading = 'pending';
    });
    // 接口請求返回
    builder.addCase(getBasicInfo.fulfilled, (state, { payload }) => {
      state.loading = 'fulfilled';
      // 這里處理數(shù)據(jù)
      state.userPoints = payload.userPoints;
      state.banner = payload.banner;
      state.quickEntrance = payload.quickEntrance;
    });
    builder.addCase(getBasicInfo.rejected, (state, action) => {
      state.loading = 'failure';
    });
  },
});

// actions
export const { increment, decrement, setValue } = homeSlice.actions;

// reducers
export default homeSlice.reducer;

3.store/index.js

//  輔助函數(shù)
import { configureStore, combineReducers } from '@reduxjs/toolkit';
import homeSlice from '../Home/homeSlice';

const reducer = combineReducers({
  home: homeSlice,
});

const store = configureStore({
  reducer,
});

export default store;

4.組件Home/home.jsx

import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { getBasicInfo, setValue } from './homeSlice';


export default function Home() {
  const { value, loading, userPoints, quickEntrance, banner }= useSelector(state => state.home);
  const dispatch = useDispatch();

  // 防抖
  const getBasicInfo = () => {
      dispatch(getBasicInfo({ userinfo: 1234567 }));
       dispatch(setValue(2));
  };

  console.log(value);
  return (
    <div>{value}</div>
  );
}

5.出口index.js

import React from 'react';
import ReactDOM from 'react-dom';
// 跨組件狀態(tài)共享
import { Provider } from 'react-redux';
import BasicRoute from './router';
import store from './store';
import './assets/style/index.less';
// ios scrollTo平滑移動兼容
import smoothscroll from 'smoothscroll-polyfill';
smoothscroll.polyfill();
import '@/styles/base.less';
import 'zarm/dist/zarm.css';

ReactDOM.render(
  <>
    <Provider store={store}>
      <BasicRoute />
    </Provider>
  </>,
  document.querySelector('#app')
);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容