exportinterfaceConfigureStoreOptions<S=any,Aextends Action = AnyAction,Mextends Middlewares<S>= Middlewares<S>>{/**
* A single reducer function that will be used as the root reducer, or an
* object of slice reducers that will be passed to `combineReducers()`.
*/
reducer: Reducer<S,A>| ReducersMapObject<S,A>;/**
* An array of Redux middleware to install. If not supplied, defaults to
* the set of middleware returned by `getDefaultMiddleware()`.
*/
middleware?:((getDefaultMiddleware: CurriedGetDefaultMiddleware<S>)=>M)|M;/**
* Whether to enable Redux DevTools integration. Defaults to `true`.
*
* Additional configuration can be done by passing Redux DevTools options
*/
devTools?:boolean| DevToolsOptions;/**
* The initial state, same as Redux's createStore.
* You may optionally specify it to hydrate the state
* from the server in universal apps, or to restore a previously serialized
* user session. If you use `combineReducers()` to produce the root reducer
* function (either directly or indirectly by passing an object as `reducer`),
* this must be an object with the same shape as the reducer map keys.
*/
preloadedState?: PreloadedState<CombinedState<NoInfer<S>>>;/**
* The store enhancers to apply. See Redux's `createStore()`.
* All enhancers will be included before the DevTools Extension enhancer.
* If you need to customize the order of enhancers, supply a callback
* function that will receive the original array (ie, `[applyMiddleware]`),
* and should return a new array (such as `[applyMiddleware, offline]`).
* If you only need to add middleware, you can use the `middleware` parameter instead.
*/
enhancers?: StoreEnhancer[]| ConfigureEnhancersCallback;}
创建store.ts导出store :
exportconst store =configureStore({
reducer:{
counter: counterReducer,},});
const reducer =createReducer({},(builder)=>{// actionCreator.toString() will automatically be called here// also, if you use TypeScript, the action type will be correctly inferred
builder.addCase(actionCreator,(state, action)=>{})// Or, you can reference the .type field:// if using TypeScript, the action type cannot be inferred that way
builder.addCase(actionCreator.type,(state, action)=>{})})
creatApi
creatApi可以返回自定义hook,reducer,reducer前缀,中间件:
// Need to use the React-specific entry point to import createApiimport{ createApi, fetchBaseQuery }from'@reduxjs/toolkit/query/react'import{ Pokemon }from'./types'// Define a service using a base URL and expected endpointsexportconst pokemonApi =createApi({
reducerPath:'pokemonApi',
baseQuery:fetchBaseQuery({ baseUrl:'https://pokeapi.co/api/v2/'}),endpoints:(builder)=>({
getPokemonByName: builder.query<Pokemon,string>({query:(name)=>`pokemon/${name}`,}),}),})// Export hooks for usage in functional components, which are// auto-generated based on the defined endpointsexportconst{ useGetPokemonByNameQuery }= pokemonApi
import{ configureStore }from'@reduxjs/toolkit'// Or from '@reduxjs/toolkit/query/react'import{ setupListeners }from'@reduxjs/toolkit/query'import{ pokemonApi }from'./services/pokemon'exportconst store =configureStore({
reducer:{// Add the generated reducer as a specific top-level slice[pokemonApi.reducerPath]: pokemonApi.reducer,},// Adding the api middleware enables caching, invalidation, polling,// and other useful features of `rtk-query`.middleware:(getDefaultMiddleware)=>getDefaultMiddleware().concat(pokemonApi.middleware),})// optional, but required for refetchOnFocus/refetchOnReconnect behaviors// see `setupListeners` docs - takes an optional callback as the 2nd arg for customizationsetupListeners(store.dispatch)
在组件中使用类似于umi的useRequest:
import*as React from'react'import{ useGetPokemonByNameQuery }from'./services/pokemon'exportdefaultfunctionApp(){// Using a query hook automatically fetches data and returns query valuesconst{ data, error, isLoading }=useGetPokemonByNameQuery('bulbasaur')// Individual hooks are also accessible under the generated endpoints:// const { data, error, isLoading } = pokemonApi.endpoints.getPokemonByName.useQuery('bulbasaur')return(<div className="App">{error ?(<>Oh no, there was an error</>): isLoading ?(<>Loading...</>): data ?(<><h3>{data.species.name}</h3><img src={data.sprites.front_shiny} alt={data.species.name}/></>):null}</div>)}