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

[bug] useSelector won't trigger update if value is changed back to initial one #5

Closed
xzilja opened this issue Jun 30, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@xzilja
Copy link
Contributor

xzilja commented Jun 30, 2020

Was playing around with useSelector and came across one problem. Assuming use case below (also available at https://codesandbox.io/s/zealous-visvesvaraya-tvvn7?file=/src/App.tsx)

If you start with initial value of pageOne, change it to pageTwo and then try to change it back to pageOne component is not re-rendered / no update happens. However toggling between pageTwo and pageThree works fine. You can check this in codesandbox mentioned above.

State

import { newRidgeState } from "react-ridge-state";

export interface RouterState {
  page: string;
}

const { set, useValue, useSelector } = newRidgeState<RouterState>({
  page: "pageOne"
});

export const useRouterValue = useValue;
export const useRouterSelector = useSelector;

export function push(page: RouterState["page"]) {
  set({ page });
}

Component

import React from "react";
import { useRouterValue, useRouterSelector, push } from "./routerState";

export default function App() {
  // This Works
  // const { page } = useRouterValue();

  // This Doesn't work
  const page = useRouterSelector(s => s.page);

  return (
    <div>
      <button onClick={() => push("pageOne")}>Page One</button>
      <button onClick={() => push("pageTwo")}>Page Two</button>
      <button onClick={() => push("pageThree")}>Page Three</button>

      {page === "pageOne" && <h1>Page One</h1>}
      {page === "pageTwo" && <h1>Page Two</h1>}
      {page === "pageThree" && <h1>Page Three</h1>}
    </div>
  );
}
@xzilja xzilja changed the title [bug] useSelector won't trigger update if value changes to initial one [bug] useSelector won't trigger update if value is changed back to initial one Jun 30, 2020
@RichardLindhout
Copy link
Member

Thanks for the reproduction. I'm looking into this issue

@RichardLindhout
Copy link
Member

I thinks it's because I forgot a dependency in the callback I will first add more safety measures with eslint to prevent this in future!

@RichardLindhout
Copy link
Member

Added test + it's fixed now :) https://codesandbox.io/s/bold-wu-9x8mx?file=/src/index.tsx

@RichardLindhout RichardLindhout added the bug Something isn't working label Jul 16, 2020
@RichardLindhout RichardLindhout self-assigned this Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants