briskml / brisk-reconciler

React.js-like reconciler implemented in OCaml/Reason
MIT License
131 stars 17 forks source link

Potential bug: Passing items from stateful component to receiving reducer #83

Open lessp opened 4 years ago

lessp commented 4 years ago

Not sure if this is a bug or not, but when passing items from one component to another one ends up with no items, despite them being available in e.g. the initialState-function.

let initialState = items => {
  Console.log(("We've got some items here:", items));
  items;
};

let%component childItem = (~items, ()) => {
  let%hook (allItems, setItems) =
    Hooks.reducer(~initialState=initialState(items), (value, _) => value);

  // Works when using the effect-hook
  /* let%hook () = */
  /*   Hooks.effect( */
  /*     OnMountAndIf((!=), items), */
  /*     () => { */
  /*       setItems(items); */

  /*       None; */
  /*     }, */
  /*   ); */

  <View>
    {allItems
     |> List.map(item => <Text style=Style.[color(Colors.red)] text=item />)
     |> React.listToElement}
  </View>;
};

let%component container = () => {
  let%hook (items, setItems) = React.Hooks.state([]);

  let%hook () =
    Hooks.effect(
      OnMount,
      () => {
        setItems(_prev => ["One", "Two", "Three"]);

        None;
      },
    );

  <childItem items />;
};