【问题标题】:Typescript error connecting class component to the reducer将类组件连接到减速器的打字稿错误
【发布时间】:2021-12-21 14:42:47
【问题描述】:

我有一个连接到 redux rootReducer 的类组件,但我在 mapPropsToState 部分收到一个 TS 错误,说明:Property 'unit' does not exist on type 'DefaultRootState'

在组件上失败的部分(第一个 {unit})用上面的错误下划线:

export default connect(
  ({ unit }) => ({ unit }),
  { fetchRegisterAll }
)(TabNavUnits);

我的根减速器似乎没问题:

import { combineReducers } from 'redux'
import auth from './auth'
import person from './person'
import room from './room'
import unit from './unit'
import scenario from './scenario'
import data from './data'

const rootReducer = combineReducers({
  auth,
  person,
  room,
  unit,
  scenario,
  data
})

export type RootState = ReturnType<typeof rootReducer>

【问题讨论】:

    标签: reactjs typescript redux


    【解决方案1】:

    我想我已经明白了。我需要导入 RootState 接口并将单元声明为 RootState 类型,如下所示:

    export default connect(
      ({ unit }: RootState) => ({ unit }),
      { fetchRegisterAll }
    )(TabNavUnits);
    

    【讨论】:

    猜你喜欢
    • 2021-03-20
    • 2021-09-03
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多