Skip to content

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

@lessp

Description

@lessp

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 />;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions