创建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
最佳答案
您缺少 reducer 。您可以为它添加一个占位符,如下所示:
let make = (_children) => {
...component,
initialState: () => { ... },
reducer: ((), _state) => ReasonReact.NoUpdate,
render: (self) => { ... }
};
请注意,这是错误消息中的第三个可能原因:“... Action 相同(例如, reducer )”。使用上述占位符,该操作将被推断为
unit
。