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

Stop component re-rendering with useSelector #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

GeraldHost
Copy link

As per this ticket: #18

I've tested this solution and it seems to fix the problem with the re-renders.

@RichardLindhout
Copy link
Member

Hey very cool PR, I forgot about it:
Will this still work:
#13

BTW esm build etc are very nice

@GeraldHost
Copy link
Author

@RichardLindhout let me check, I also just realised I'm committing the build which is not ideal so will sort that too.

Comment on lines +111 to +116
const [l, s] = R.useState<TSelected>();
sub((nv) => {
const v = se(nv);
if (!eq(v, l)) s(v);
});
return l;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's not initialized properly. Recommend updating this useState(() => selector(v)); and making a new PR since the code has divergged.

  function useSelector(selector, comparator = equ) {
    const [state, setState] = useState(() => selector(v));
    subscribe(() => {
      const selectedValue = selector(v);
      if (!comparator(selectedValue, state)) setState(selectedValue);
    });
    return state;
  }

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

Successfully merging this pull request may close these issues.

3 participants