我对 ReasonML 很陌生。我能够使用 ReasonReact 成功创建无状态组件,但我还没有弄清楚如何向组件添加自定义方法(例如Next.js' staticgetInitialProps
)。
尝试getInitialProps
在组件上定义方法时,我收到一个The field getInitialProps does not belong to type ReasonReact.componentSpec
错误。
我应该如何在 React 组件上添加这个定义这个自定义方法?
零件
let str = ReasonReact.stringToElement;
let component = ReasonReact.statelessComponent("Index");
let make = (~items: Listings.items, _children) => {
...component,
getInitialProps: () =>
Js.Promise.(
Endpoints.fetchListings()
|> then_(Fetch.Response.json)
|> then_((json) => Js.Json.decodeArray(json) |> resolve)
),
render: (_self) =>
<div>
(List.length(items) > 0 ? <Listings items /> : <Loading />)
</div>
};
let default =
ReasonReact.wrapReasonForJs(
~component,
(jsProps) => make(~items=jsProps##items, [||])
);
错误
We've found a bug for you!
/Users/davidcalhoun/Sites/web/evergreen-roots/pages/Index.re 7:3-17
5 │ let make = (~items: Listings.items, _children) => {
6 │ ...component,
7 │ getInitialProps: () =>
8 │ Js.Promise.(
9 │ Endpoints.fetchListings()
This record expression is expected to have type
ReasonReact.componentSpec ('a, 'b, 'c, 'd, 'e)
The field getInitialProps does not belong to type ReasonReact.componentSpec