【发布时间】:2018-11-17 03:40:59
【问题描述】:
我正在尝试在 Next.js 库中使用 withRouter HOC 助手。
import { withRouter } from 'next/router'
const MyComponent = withRouter(({ router }) => ())
这会导致流程错误:
[flow] property `router` is missing in props [1]. (References: [1])
即使我为 Next.js 安装了流类型,并且 withRouter 函数具有以下签名:
withRouter: < T > (Component: React$ComponentType < (T & {
router: Router
}) > ) => class React$Component
我认为 flow 会发现 router 是由 withRouter 注入的,但似乎不是这样?如何修复这种类型的错误?
我尝试导入路由器类型并将其声明为道具:
import { Router } from 'next/router'
type Props = { router: Router }
const MyComponent = withRouter(({ router: Router }: Props) => ())
这消除了错误,但现在我得到了一个不同的错误:
Property router is missing in props [1] but exists in object type [2].
[1] 61│ {typeof query.post !== 'undefined' && <Post />}
[2] 29│ const Basic = withRouter(({ router }: { router: Router }) => (
【问题讨论】:
-
我不是这里的专家,希望你能听到真正知道这是如何工作的人的意见,但通常在我处理过这种道具注入的地方,我仍然必须定义注入的道具在我用于组件道具的流类型中。
-
你知道如何从 flow-typed/npm/next_v7.x.x.js 导入路由器类型吗?
-
我已经通过声明额外的道具更新了问题,但我现在在他的呼叫站点仍然遇到不同的错误
标签: javascript flowtype next.js