【问题标题】:How to define type for props如何定义道具的类型
【发布时间】:2017-12-06 06:37:03
【问题描述】:

代码:

let component = ReasonReact.statelessComponent("Page");
type matchParams = {.
  id: int
};
type match = {.
  params: matchParams
};
type userProps = {.
  match: match
};
let home = <Home />;
let user = (~props:userProps) => <User id=props.match.params.id />;
let make = (_children) => {
  ...component,
  render: (_self) =>
    <div> <Route key="home" exact=false path="/home" component=home />
    <Route key="user" exact=false path="/user/:id" component=user /> </div>
};

输出:

未绑定记录字段匹配

在行

let user = (~props:userProps) => <User id=props.match.params.id />;

如何定义类型,我做错了什么?

【问题讨论】:

    标签: compiler-errors reason


    【解决方案1】:

    该错误是由match 是保留关键字引起的。不过,您应该收到更好的错误消息,我认为这是 Reason 中的错误。解决这个问题,如果需要在JS端编译成match,可以改用_match,否则就改个名字就行了。

    您还(可能)还有其他一些问题:

    1. let home = &lt;Home /&gt; 不是像user 这样的函数。可能需要let home = () =&gt; &lt;Home/&gt;

    2. 您将记录类型定义为props,将由component 函数提供给您。但是您可能会得到一个 JS 对象而不是记录。请参阅Reason guide,其中解释了差异。

    【讨论】:

    • 但是 match 是 react-router 中的属性,如果我不能使用 match 词,我该如何使用它
    • 如上所述,使用_match。 BuckleScript 将从生成的代码中删除前导下划线。见bucklescript.github.io/bucklescript/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    相关资源
    最近更新 更多