`self.send` 出现 ReasonReact 编译时错误

`self.send` on ReasonReact Compile-time Error

鉴于以下情况:

$cat src/Greeting.re
let component = ReasonReact.reducerComponent("Greeting");

type action =
 | Click;

type state = {
    count: int
};

let make = (_children) => {
  ...component,
  initialState: () => {count: 0},
  reducer: (action, state) =>
    ReasonReact.Update({count: state.count + 1}),
  render: (self) => {
     let message = "Clicked " ++ string_of_int(self.state.count) ++ "x";
        <div>
          <button
            onClick={_event => self.send(Click)}
          />
          {ReasonReact.stringToElement(message)}
        </div>
  }
};

我收到以下编译时错误:

  17 ┆ <div>
  18 ┆   <button
  19 ┆     onClick={_event => self.send(Click)}
  20 ┆   />
  21 ┆   {ReasonReact.stringToElement(message)}

  This record expression is expected to have type
    ReasonReact.componentSpec (state,  'a,  'b,  'c,  'd)
  The field send does not belong to type ReasonReact.self

ninja: build stopped: subcommand failed.
>>>> Finish compiling(exit: 1)

我不明白。有人可以解释错误是什么以及如何解决吗?

您必须将 let component = ReasonReact.reducerComponent("Greeting"); 行放在 make 声明之前,如下所示:

…
let component = ReasonReact.reducerComponent("Greeting");

let make = (_children) => {
  ...component,
  initialState: () => {count: 0},
…

原因是 reducer 元素的类型是根据其他类型(即 stateaction)推断的,因此它需要能够 "see" 它们声明时。

此外,作为记录,您应该在 bsb 输出中看到关于此的警告:

Is this a ReasonReact reducerComponent or component with retained props? If so, is the type for state, retained props or action declared after the component declaration? Moving these types above the component declaration should resolve this!

再次尝试 运行 npm run start 以查看警告!

我有类似的问题,我解决了将 self.send 更改为 self.reduce,尝试更改:

 <button
        onClick={_event => self.send(Click)}
 />

在:

<button
  onClick=(self.reduce(_event => Click))
/>

我不确定,但也许 self.reduce 是旧的 API,所以我们可能有旧版本的东西

编辑: 我只是将 reason-react 更新为“0.3.1”,现在使用 send