Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render issue with the state hook #74

Open
JulesGuesnon opened this issue Mar 25, 2020 · 1 comment
Open

Render issue with the state hook #74

JulesGuesnon opened this issue Mar 25, 2020 · 1 comment

Comments

@JulesGuesnon
Copy link

Hello !
I wrote a component like this:

let%component make = (~render, ()) => {
  let%hook (currentRoute, setCurrentRoute) =Hooks.state(Store.route^);

  let%hook () =
    Hooks.effect(
      OnMount,
      () => {
        Store.subscribe((_, newRoute) => setCurrentRoute(_ => newRoute));
        None;
      },
    );

  <View> {render(state.currentRoute)} </View>;
};

It's supposed to rerender every time the route update. It works on the first update of my route, and then the component doesn't update at all. I added some log inside the setCurrentRoute, it is triggered, but component still not rerender.

Someone suggested that I make it with a reducer and it worked perfectly:

type action =
  | Route(string);

type state = {currentRoute: string};

let reducer = (action, state) => {
  switch (action) {
  | Route(route) => {currentRoute: route}
  };
};

let%component make = (~render, ()) => {
  let%hook (state, dispatch) =
    Hooks.reducer(~initialState={currentRoute: Store.route^}, reducer);

  let%hook () =
    Hooks.effect(
      OnMount,
      () => {
        Store.subscribe((_, newRoute) => dispatch(Route(newRoute)));
        None;
      },
    );

  <View> {render(state.currentRoute)} </View>;
};
@wokalski
Copy link
Member

Thank you. There wasn't an issue for this.

ericluap added a commit to ericluap/revery that referenced this issue May 25, 2020
Using reducer instead of state hook because of possible bug with state
hooks (briskml/brisk-reconciler#74).
glennsl pushed a commit to revery-ui/revery that referenced this issue May 27, 2020
* Add isFocused

* Check focus with Focus.isFocused

* Fix focus not blurring first

* Switch from reducer to state hook

* Remove old reducer actions

* Add interface for Focus

* Stick with reducer

Using reducer instead of state hook because of possible bug with state
hooks (briskml/brisk-reconciler#74).

* Use Focus.dispatch for events

* Separate function for internal to external event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants