0

鉴于以下情况:

$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)

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

4

2 回答 2

5

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

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

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

原因是 reducer 元素的类型是根据其他类型(即stateand action)推断出来的,因此它需要在声明时能够“看到”它们。

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

这是一个 ReasonReact reducerComponent 还是带有保留 props 的组件?如果是这样,是在组件声明之后声明了 state、retained props 或 action 的类型 吗?将这些类型移到组件声明之上应该可以解决这个问题!

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

于 2018-01-14T23:23:26.023 回答
0

我有类似的问题,我解决了用 self.reduce 改变 self.send,尝试改变:

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

在:

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

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

编辑:我只是将原因反应更新为“0.3.1”,现在使用发送

于 2018-01-28T18:41:49.920 回答