Compiler errors 如何定义道具的类型
代码:Compiler errors 如何定义道具的类型,compiler-errors,reason,Compiler Errors,Reason,代码: let component=ReasonReact.statelessComponent(“页面”); 类型matchParams={。 id:int }; 类型匹配={。 参数:匹配参数 }; 键入userProps={。 比赛:比赛 }; 让家=; 让用户=(~props:userProps)=>; 让make=(_children)=>{ …组件, 渲染:(_self)=> }; 输出: 未绑定记录字段匹配 在行 let component = ReasonReact.state
let component=ReasonReact.statelessComponent(“页面”);
类型matchParams={。
id:int
};
类型匹配={。
参数:匹配参数
};
键入userProps={。
比赛:比赛
};
让家=;
让用户=(~props:userProps)=>;
让make=(_children)=>{
…组件,
渲染:(_self)=>
};
输出:
未绑定记录字段匹配
在行
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)=>;
如何定义类型,我做错了什么 该错误是由于
匹配
是保留关键字引起的。你应该得到一个更好的错误消息,我会认为这是一个错误的原因。为了解决这个问题,如果您需要在JS端将其编译为match
,您可以使用\u match
,否则只需使用不同的名称即可
您(可能)还有一些其他问题:
let home=
不是像user
那样的函数。它可能需要是let home=()=>
组件
函数将提供给您的道具。但您可能会得到一个JS对象,而不是一条记录。请参见解释差异的
但是match是react路由器中的属性,如果我不能使用上面提到的match这个词,我该如何使用它呢?请使用
\u match
。BuckleScript将从生成的代码中删除前导下划线。看见
let user = (~props:userProps) => <User id=props.match.params.id />;