2

我正在使用 ReasonReact 创建网站,但在使用普通组件时遇到此错误消息。有谁知道发生了什么?

module Component1 = {
  let component = ReasonReact.statelessComponent("Component1");
  let make = () => {...component, render: self => <div />};
};

module Component2 = {
  let component = ReasonReact.statelessComponent("Component1");
  let make = () => {
    ...component,
    render: self => <div> <Component1 /></div>, /*error on compenent1*/ 
  };

这是错误消息:


(
  React.component('props),
  'props
) => React.element
<root>/node_modules/reason-react/src/React.re

Error: This expression has type
         unit =>
         ReasonReact.componentSpec(ReasonReact.stateless,
                                    ReasonReact.stateless,
                                    ReasonReact.noRetainedProps,
                                    ReasonReact.noRetainedProps,
                                    ReasonReact.actionless)
       but an expression was expected of type
         React.component(unit) = unit => React.element
       Type
         ReasonReact.componentSpec(ReasonReact.stateless,
                                    ReasonReact.stateless,
                                    ReasonReact.noRetainedProps,
                                    ReasonReact.noRetainedProps,
                                    ReasonReact.actionless)
       is not compatible with type React.element 

4

1 回答 1

1

问题似乎是您正在使用配置为使用 JSX 版本 3 和为 JSX 版本 2 设计的组件的项目。

在 ReasonReact 0.7.0 中引入了 JSX 版本 3,以及用于定义支持钩子的反应组件的新方法,但只要您将项目配置为使用 JSX 版本 2,它仍然支持您正在使用的方法。如果这是似乎是新项目,我建议使用新的组件样式,您的代码看起来像这样:

module Component1 = {
  [@react.component]
  let make = () =>
    <div />;
};

module Component2 = {
  [@react.component]
  let make = () =>
    <div> <Component1 /> </div>;
};

或者,您可以通过在 中指定以下内容来继续使用旧样式的组件和 JSX 版本 2 bsconfig.json

{
  ...
  "reason": {"react-jsx": 2}
}

有关详细信息,请参阅ReasonReact 0.7.0 上的博客文章

于 2019-06-06T10:20:38.110 回答