Skip to content

Instantly share code, notes, and snippets.

@nickpascucci
Last active June 5, 2018 21:26
Show Gist options
  • Save nickpascucci/727f37681b2444ecc043feb9497c653e to your computer and use it in GitHub Desktop.
Save nickpascucci/727f37681b2444ecc043feb9497c653e to your computer and use it in GitHub Desktop.
Broken ReasonReact component
/src/ui-re/src/PageHeader.re 8:17-58
6 _ | Select('a);
7 _
8 _ let component = ReasonReact.reducerComponent("PageHeader");
9 _
10 _ let make = (~orgName, ~productName, ~tabs : list(('a, string)), ~onTab
Change : ('a => unit), _children) => {
This expression's type contains type variables that can't be generalized:
ReasonReact.componentSpec(state('_a), ReasonReact.stateless,
ReasonReact.noRetainedProps,
ReasonReact.noRetainedProps, action('_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
ninja: build stopped: subcommand failed.
>>>> Finish compiling(exit: 1)
[%bs.raw {|require('./Header.css')|}];
type state('a) = {selected: 'a};
type action('a) =
| Select('a);
let component = ReasonReact.reducerComponent("PageHeader");
let make = (~orgName, ~productName, ~tabs : list(('a, string)), ~onTabChange : ('a => unit), _children) => {
...component,
initialState: () => {selected: (tabs |> List.hd |> fst)},
reducer: (action, state) =>
switch(action) {
| Select(a) => ReasonReact.UpdateWithSideEffects({selected: a}, self => onTabChange(self.state.selected))
},
render: self => {
<nav className="bx--cloud-header">
<div className="bx--cloud-header__wrapper">
<button className="bx--cloud-header__app-menu" type_="button">
<svg width="20" height="20" viewBox="0 0 20 20">
<path d="M3 4h14v1H3zm0 6h14v1H3zm0 6h14v1H3z"></path>
</svg>
</button>
<a href="#" className="bx--cloud-header-brand">
<h4 className="bx--cloud-header-brand__text">
(ReasonReact.string(orgName ++ " "))
<span>
(ReasonReact.string(productName))
</span>
</h4>
</a>
<ul className="bx--cloud-header-list">
<li className="bx--cloud-header-list__item">
(ReasonReact.array(
Array.of_list(List.map(((a, l)) =>
<a className="bx--cloud-header-list__link"
href="#" key=l
onClick=(_e => self.ReasonReact.send(Select(a)) )>
(ReasonReact.string(l))
</a>, tabs))))
</li>
</ul>
</div>
</nav>
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment