简体   繁体   English

在 TypeScript 中键入 Redux 工具包的存储

[英]Typing Redux toolkit's store in TypeScript

I'm trying to get started using Redux in React + TypeScript so I am following the Redux Toolkit's tutorials and try implementing it in a sample app.我正在尝试开始在 React + TypeScript 中使用 Redux,因此我正在关注 Redux Toolkit 的教程并尝试在示例应用程序中实现它。

I am having a lot of difficulty with typings concerning the store and the mappStateToProps function parameters.我在输入有关 store 和mappStateToProps函数参数时遇到很多困难。

I configured the store with a slice as follows:我用切片配置了商店,如下所示:

import { configureStore } from '@reduxjs/toolkit'

import linksReducer from '../features/links/linksSlice'

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

export default store
export type AppStore = typeof store

And I connected a component like so:我像这样连接了一个组件:

import React from 'react'
import { connect, ConnectedProps } from 'react-redux'

import { AppStore } from '../../features/appStore'
import { deleteLink } from '../../features/links/linksSlice'

import LinkCard from './LinkCard'
import Link from '../../models/Link'
import EmptyList from './EmptyList'

const mapStateToProps = (state: any) => ({
  links: state.links,
})
const mapDispatchToProps = { deleteLink }
const connector = connect(mapStateToProps, mapDispatchToProps)

const LinksGrid = (props: ConnectedProps<typeof connector>) => {
  //  ...
}

export default connector(LinksGrid)

As you can see, I used the any type for the first argument of the mapStateToProps function because I can't find out how I am supposed to type it.如您所见,我使用any类型作为mapStateToProps函数的第一个参数,因为我不知道应该如何输入它。 Using any , I have no problem running the application so I assume I only have a typing problem.使用any ,我运行应用程序没有问题,所以我假设我只有打字问题。

The official documentation of RTK gives only an example of using combineReducers instead of configureStore , and it uses TS's ReturnType<T> which can only work on function but of course the store variable is not a function so it doesn't work. RTK 的官方文档只给出了使用combineReducers而不是configureStore的例子,并且使用了 TS 的ReturnType<T> ,它只能作用于函数,当然store变量不是函数所以它不起作用。
I have also read the Redux and React Redux documentation but I still can't figure what to do.我还阅读了 Redux 和 React Redux 文档,但我仍然不知道该怎么做。

I am sure this is actually simple but here I am... Can anyone help me get that typing working?我确信这实际上很简单,但我在这里......谁能帮我打字?

Thanks :)谢谢 :)

Both the Redux Toolkit Advanced Tutorial and Usage with TypeScript docs pages show you how to do this correctly. Redux Toolkit Advanced TutorialUsage with TypeScript文档页面都向您展示了如何正确执行此操作。

First, it's not the type of the store that's important here.首先,重要的不是商店的类型。 It's the type of the store state .它是store state的类型。

If you use combineReducers() yourself, you can define that type as:如果您自己使用combineReducers() ,则可以将该类型定义为:

const rootReducer = combineReducers({
    first: firstReducer,
    second: secondReducer,
});

type RootState = ReturnType<typeof rootReducer>

However, since you're using the shorthand setup form by passing the slice reducers to configureStore (which calls combineReducers() internally), you can do the same thing by checking the return type of store.getState instead:但是,由于您通过将切片combineReducers()传递给configureStore (在内部调用combineReducers() )来使用速记设置表单,因此您可以通过检查store.getState的返回类型来执行相同的store.getState

const store = configureStore({
    reducer: {
        first: firstReducer,
        second: secondReducer,
    }

});

type RootState = ReturnType<typeof store.getState>

Then, as shown in the docs, you can import the RootState type into your component file and use it as the type in your mapState function:然后,如文档中所示,您可以将RootState类型导入到组件文件中,并将其用作mapState函数中的类型:

import {RootState} from "app/store";

const mapStateToProps = (state: RootState) => ({
  links: state.links,
})

It works me:它对我有用:

store?: EnhancedStore<{ default: RootState }, AnyAction, [ThunkMiddleware]>;

additionally i have added redux-thunk for ThunkMiddleware另外我为ThunkMiddleware添加了redux-thunk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM