Skip to content

Example upto/before reducerComponent Heading doesnt run #3

@idkjs

Description

@idkjs

Might not be supposed to run at this point...

This, i think, is the code up to that point:


/* define type for state
Note that the state type must be defined before the call
to ReasonReact.reducerComponent or you'll get an error
saying something like "The type constructor state would
escape its scope". Since RepoData might be null and we need to account for
that type as a possibility, we have to use Reason's option type,
then in initialstate wrap our record in Some() to let compiler no that this might be a data
or null. The option type as to variants, Some or None. Some when a value is present
None when no value is present.
*/

type state = {repoData: option(RepoData.repo)};

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

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

/* use switch statement instead of render on initialState so that we can 
account for each result in our repo data option type, the render that arg
in the div. RepoItem module expects a repo, so Some gives us a repo, pass that
value to RepoItem ~repo named arg.
*/
let make = (_children) => {
  ...component,
  initialState: () => {
    repoData: Some(dummyRepo)
  },
  render: (self) => {
    let repoItem =
      switch (self.state.repoData) {
      | Some(repo) => <RepoItem repo=repo />
      | None => ReasonReact.stringToElement("Loading")
      };
    <div className="App">
      <h1>{ReasonReact.stringToElement("Reason Projects")}</h1>
      repoItem
    </div>
  }
};

it produces this error:

./src/index.re
Module build failed: Error: We've found a bug for you!
  /Users/shingdev/code/REASON/reason-repo-list/src/app.re 15:17-51
  
  13 │ type state = {repoData: option(RepoData.repo)};
  14 │ 
  15 │ let component = ReasonReact.reducerComponent("App");
  16 │ 
  17 │ let dummyRepo: RepoData.repo = {
  
  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:
  https://realworldocaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism    at <anonymous>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions