如何在 ReasonReact 中解决 - "This has type: (ReasonReact.reactElement, string) => unit But somewhere wanted: at <anonymous>actElement

How to solve in ReasonReact - "This has type: (ReasonReact.reactElement, string) => unit But somewhere wanted: at <anonymous>actElement

我有一个非常紧凑的 ReasonReact reducer 组件,它有一个组件、initialState、reducer、action 和 render 函数如下:

type actions =
  | DoNothing;

let component = ReasonReact.reducerComponent("ShowHide");

let make = (~name, _children) => {
  ...component,
  initialState: () => 0, /* here, state is an `int` */
  reducer: (action, state) =>
    switch action {
      | DoNothing => ReasonReact.NoUpdate;
    },
  render: (self) => {
    let greeting =
      "Hello: " ++ name ++ ". You've clicked the button " ++ string_of_int(self.state) ++ " time(s)!";
    <div></div>
  }
};

我正在尝试使用 ReactDOMRe.renderToElementWithId 函数在我的 app.re 文件中呈现:

<div id = "RenderShowHide"></div>
        ReactDOMRe.renderToElementWithId(<ShowHide name="hello" />, "RenderShowHide")

然而,Reason/Bucklescript 编译器报错如下:

This has type:
    (ReasonReact.reactElement, string) => unit
  But somewhere wanted:
    at <anonymous>actElement

但是,我很难理解 actElement 是什么。关于 actElement 是什么以及我如何解决上述问题的任何建议,将不胜感激。谢谢。

我试过你post编辑的回购:https://github.com/CharlieGreenman/reason-react-razroo

npm install
bsb -make-world

我收到以下错误消息:

  We've found a bug for you!
  /Users/yawar/src/reason-react-razroo/src/app.re 16:9-40

  14 ┆ </div>
  15 ┆ <p className="App-intro">
  16 ┆   ReactDOMRe.renderToElementWithId(<ShowHide name="hello"/>, "index")
  17 ┆   (ReasonReact.stringToElement("To get started, edit"))
  18 ┆   <code> (ReasonReact.stringToElement(" src/app.re ")) </code>

  This has type:
    (ReasonReact.reactElement, string) => unit
  But somewhere wanted:
    ReasonReact.reactElement

看起来您的构建工具中的某些东西吞没了您的错误消息的一部分。主要问题在l。 16;如果你摆脱那条线,错误就会消失。如果要渲染 ShowHide 组件,则将行更改为文字 JSX,而不是对 ReactDOMRe.renderToElementWithId.

的调用

我有两个更一般的建议;尝试坚持使用 bsb-provided React 骨架项目,除非你是 expert-level,因为它更简单并且得到更好的支持:

bsb -init my-project -theme react

并且,尝试 post 整个错误消息以备将来出错,从“我们为您找到了一个错误!”开始线。这将有助于诊断很多。