ReasonReact编译时错误的`self.send`

Kev*_*ith 0 reason reason-react

鉴于以下内容:

$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>
  }
};
Run Code Online (Sandbox Code Playgroud)

我得到以下编译时错误:

  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)
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*mes 5

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

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

let make = (_children) => {
  ...component,
  initialState: () => {count: 0},
…
Run Code Online (Sandbox Code Playgroud)

这样做的原因是reducer元素的类型是基于其他类型(即stateaction)推断的,因此它需要能够在声明它时"看到"它们.

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

这是一个ReasonReact reducerComponent还是具有保留道具的组件?如果是这样,组件声明声明的状态,保留道具或动作的类型是 什么?将这些类型移到组件声明之上应解决此问题!

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