5

创建 reducerComponent 时出现错误:

代码

type state = {repoData: RepoData.repo};

let dummyRepo: RepoData.repo = {
  stargazers_count: 27,
  full_name: "jsdf/reason-react-hacker-news",
  html_url: "https://github.com/jsdf/reason-react-hacker-news"
};

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

let make = (_children) => {
  ...component,
  initialState: () => {
    repoData: dummyRepo
  },
  render: (self) => {
    <div className="App">
      <h1>{ReasonReact.stringToElement("Reason Projects")}</h1>
      <RepoItem repo={self.state.repoData} />
    </div>
  }
};

错误:

Failed to compile.

./src/index.re
Module build failed: Error: We've found a bug for you!
  /Users/me/personal/test/reason-app-shell-starter-kit/src/page1.re 9:17-53

   7 │ };
   8 │
   9 │ let component = ReasonReact.reducerComponent("Page1");
  10 │
  11 │ let make = (_children) => {

  Is this a ReasonReact reducerComponent or component with retained props?
  If so, this error will disappear after:
  - Defining the component's `make` function
  - Using the state once or annotating it with a type where it's used (e.g. render)
  - Doing the same for action (in e.g. reducer)
  - Doing the same for retained props, if any

  Here's the original error message
  This expression's type contains type variables that can't be generalized:
  ReasonReact.componentSpec
  (state,  ReasonReact.stateless,  ReasonReact.noRetainedProps,
    ReasonReact.noRetainedProps,  '_a)

  This happens when the type system senses there's a mutation/side-effect, in combination with a polymorphic value.
  Using or annotating that value usually solves it. More info:
    at <anonymous>docaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism

任何人都可以看到问题吗?

信息

我的 package.json:

{
  "name": "reason-app-shell-starter-kit",
  "version": "0.1.0",
  "homepage": "https://persianturtle.github.io/reason-app-shell-starter-kit/build",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "reason-scripts": "0.8.0"
  },

  "devDependencies": {
    "bs-jest": "^0.3.2",
    "node-sass-chokidar": "0.0.3",
    "reason-react": "^0.3.2"
  }
}

系统:

node v9.4.0
OS: osx
4

1 回答 1

4

你错过了减速机。您可以为它添加一个占位符,如下所示:

let make = (_children) => {
  ...component,

  initialState: () => { ... },
  reducer: ((), _state) => ReasonReact.NoUpdate,

  render: (self) => { ... }
};

请注意,这是错误消息中的第三个可能原因:“......操作相同(例如减速器)”。使用上述占位符,动作将被推断为unit.

于 2018-02-26T16:13:06.443 回答