javascript - 如何使用 flowjs 从到达路由器键入 Match 组件
问题描述
类型定义是:
declare export class Match<Params> extends React$Component<{|
path: string,
children: (props: {|
match: null | ({ uri: string, path: string } & Params),
location: typeof location,
navigate: NavigateFn,
|}) => React$Node,
|}> {}
像这样使用匹配:
<Match path="/agents/:id">
{({ match, navigate }) => ( [...] )
</Match>
这里match
被认为是null
按流程
如果我尝试类似
class MatchAgent extends Match<{id: string}> {}
flow 效果很好,但反应崩溃不能将 Class 作为函数调用。
const MatchAgent: Match<{id:string}> = Match;
这适用于反应,但不适用于流程:'(
有人知道我们如何用 flowjs 输入吗?谢谢
编辑:这是我的解决方法
const MatchAgent = new Match<{ id: string }>({
path: '/agents/:id',
children: ({ match, navigate }) => ([...]),
});
解决方案
如果我理解正确,您想match
成为非null
. 不幸的是,根据 Flow 类型的定义,match
可以是null
或形状的对象{ uri: string, path: string } & Params
。为了让 Flow 停止抱怨match
being null
,您需要match
通过检查它是否是来进行改进null
。例如,
import React from 'react';
declare export class Match<Params> extends React$Component<{|
path: string,
children: (props: {|
match: null | ({ uri: string, path: string } & Params),
location: any, // since location isn't defined in Try Flow
navigate: any, // since NavigateFn isn't defined in Try Flow
|}) => React$Node,
|}> {}
function App() {
return (
<Match path="/agents/:id">
{({ match, navigate }) => {
if (match === null) {
return null; // or whatever you want when it doesn't match
}
return match.id;
}}
</Match>
);
}
或者,如果您可以控制 的流类型定义(和实现)Match
,则可以null
从联合类型中删除match: { uri: string, path: string } & Params
.
推荐阅读
- php - 二叉树获取所有左/右节点
- spring - 未找到表“BATCH_JOB_INSTANCE”:org.h2.jdbc.JdbcSQLException
- css - 除非添加溢出属性,否则无法更改导航栏颜色
- java - Android Load Full Image Recyclerview Cardview
- docker - 将 docker 映像中目录中存在的文件挂载到主机中的目录
- python - 在 Python 中仅从混合字符串中提取单词
- logitech - 如何让g703 hero连接旧的罗技游戏软件?不是 G 集线器
- tensorflow - 什么是验证损失单位 Keras/Tensorflow?
- c++ - 需要帮助调试“属性解析器”!来自 HackerRank 的 C++ 问题
- python - 将宽数据框转换为 Pytorch 张量对象的正确尺寸?