国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【React】TS項目配置Redux

這篇具有很好參考價值的文章主要介紹了【React】TS項目配置Redux。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前提條件

在React中使用Redux,官方要求安裝兩個插件,Redux Toolkitreact-redux

Redux Toolkit(RTK): 官方推薦編寫Redux邏輯的方式,是一套工具的集合集,簡化書寫方式。

  1. 簡化 store 的配置方式
  2. 內(nèi)置 immer 支持可變式狀態(tài)修改
  3. 內(nèi)置 thunk 更好的異步創(chuàng)建

react-redux:用來鏈接 Redux 和 React組件的中間件。

安裝

npm i @reduxjs/toolkit react-redux

瀏覽器插件 - Redux DevTools(推薦但不強(qiáng)制使用)

下載并啟用插件
【React】TS項目配置Redux,React,Redux,react.js,javascript,ecmascript

控制臺找到redux選項
【React】TS項目配置Redux,React,Redux,react.js,javascript,ecmascript

配置

目錄結(jié)構(gòu)

src 下創(chuàng)建 store,其中 index.ts/index.js 作為modules中所有store的集合
【React】TS項目配置Redux,React,Redux,react.js,javascript,ecmascript

store/index.ts配置

import { configureStore } from '@reduxjs/toolkit'

const store = configureStore({
  reducer: {},
})

// 后續(xù)使用useSelector時參數(shù)state的類型
export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

export default store

index.ts 或 main.ts中注冊store

import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
// 添加如下
import store from './app/store'
import { Provider } from 'react-redux'

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)

至此已經(jīng)全局注冊了Redux

配置

這里以計數(shù)器為例,在store/modules中創(chuàng)建counterSlice.js/ts

同步配置

import { createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

export interface CounterState {
  value: number
}

const initialState: CounterState = {
  value: 0,
}


export const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state:CounterState) => {
      state.value += 1
    },
    decrement: (state:CounterState) => {
      state.value -= 1
    },
  },
})

export default counterSlice.reducer

異步配置

import { createSlice,createAsyncThunk } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

export interface CounterState {
  value: number
}
const initialState: CounterState = {
  value: 0,
}

export const counterSlice = createSlice({
  name: 'counter',
  initialState,
  // 同步操作
  reducers: {
   	......
  },
  // 異步操作
  extraReducers(builder){
    // 這里的payload就是incrementAsync的返回值
    builder.addCase(incrementAsync.fulfilled,(state,{payload})=>{
      state.value += payload
    })
  }
})
// 定義異步函數(shù)
export const incrementAsync = createAsyncThunk<number>(
  "incrementAsync",
  async(p:number)=>{
  const res = await new Promise<number>(r=>{
    setTimeout(() => {
      r(p)
    }, 1000);
  })
  return res
})
export default counterSlice.reducer

store/index.ts 中注冊

import { configureStore } from "@reduxjs/toolkit";
// 引入counterSlice.ts
import counterStore  from "./modules/counterSlice";

const store = configureStore({
  reducer:{
  	// 注冊,注意名字要與counterSlice.ts中的name一致
    counter:counterStore
  }
})

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

export default store 

再次查看Redux Tools工具,已經(jīng)發(fā)現(xiàn)value
【React】TS項目配置Redux,React,Redux,react.js,javascript,ecmascript

使用

// useSelect用于選擇操作哪個store
// useDispatch用于生成實例,該實例可以調(diào)用reducers的function
import { useSelector, useDispatch } from "react-redux" 

// 引入index中的RootState類型  js項目不需要
import { AppDispatch, RootState } from "@/store" 

// 引入functions
import { decrement, increment, incrementAsync } from "@/store/modules/counterSlice" 

const Demo = () => {
  const count = useSelector((state: RootState) => state.counter.value)
  const dispatch: AppDispatch = useDispatch() // 異步函數(shù)必須加AppDispatch類型,否則報錯,同步可以不添加
  return (
    <>
      <div className="navbar_top"></div>
      <div>{count}</div>
      <button onClick={() => dispatch(increment({ value: 2 }))}>+2</button>
      <button onClick={() => dispatch(incrementAsync(2))}>+2</button>
      <button onClick={() => dispatch(decrement())}>-1</button>
    </>
  )
}
export default Demo

操作后通過工具發(fā)現(xiàn)value已經(jīng)改變
【React】TS項目配置Redux,React,Redux,react.js,javascript,ecmascript文章來源地址http://www.zghlxwxcb.cn/news/detail-814947.html

到了這里,關(guān)于【React】TS項目配置Redux的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包