鉴于以下内容:
$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)
我不明白。有人可以解释错误是什么以及如何解决它?
答案 0 :(得分:5)
您必须在let component = ReasonReact.reducerComponent("Greeting");
声明之前放置make
行,如下所示:
…
let component = ReasonReact.reducerComponent("Greeting");
let make = (_children) => {
...component,
initialState: () => {count: 0},
…
这样做的原因是reducer元素的类型是根据其他类型(即state
和action
)推断出来的,所以它需要能够在声明它时“看到”它们
另外,对于记录,您应该在bsb
输出中看到关于此的警告:
这是一个ReasonReact reducerComponent还是保留的组件 道具?如果是,则声明状态,保留的道具或动作的类型 在组件声明之后?将这些类型移到组件声明之上应解决此问题!
尝试再次运行npm run start
以查看警告!
答案 1 :(得分:0)
我有类似的问题,我解决了改变self.send与self.reduce,尝试改变:
<button
onClick={_event => self.send(Click)}
/>
在:
<button
onClick=(self.reduce(_event => Click))
/>
我不确定,但也许self.reduce是旧API,所以可能我们有旧版本的东西
编辑: 我只是将原因更新为“0.3.1”,现在使用send