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

Components Re-rendering #18

Open
JakeNap opened this issue Aug 5, 2021 · 4 comments
Open

Components Re-rendering #18

JakeNap opened this issue Aug 5, 2021 · 4 comments

Comments

@JakeNap
Copy link

JakeNap commented Aug 5, 2021

Given the following example my understanding is that Component should only re-render when value changes. But it seems that because useSelector internally uses use which uses useState you are essentially subscribing to the entire state update but just returning a filtered value? So Component re-renders whenever anything in state changes? Or have I misunderstood something?

// state.js
export const exampleState = newRidgeState({ count: 0, value: 0 });
setInterval(() => {
  // we are only updating the count value
  exampleState.set(x => ({...x, count: x.count + 1 }));
}, 100);
// component.js
const Component = ({ children }) => {
  console.log("Render: ExampleInner");
  // we want to subscribe to only changes to value and only rerender if value changes
  const exampleValue = exampleState.useSelector(x => x.value);
  return ( ... );
};
@RichardLindhout
Copy link
Member

The desired behaviour is not re-rendering on state updates when deep value of selector has not changed. It should work!

@JakeNap
Copy link
Author

JakeNap commented Aug 6, 2021

Hmm yeah this is what I thought but I can't see how this can be achieved. Because in the code the use function is doing:

const [state, setState] = useState();
sub(setState);

So anytime the subscriber fires it is going to cause a re-render.

Here is a code sandbox showing this:

https://codesandbox.io/s/peaceful-http-lk8bx?file=/src/App.js

Shouldn't useSelector subscribe to the state updates directly so it can filter on the value and then setState accordingly? Instead of using the use function?

@GeraldHost
Copy link

I've opened a PR that I believe fixes this issue. (Sorry for the confusing account switching 😂 Me and JakeNap are the same person).

#19

Let me know what you think? I've tested this tweak in the PR and it fixes my issue and I think it's how useSelector should handle state updates. But there maybe some context I'm missing as to why this library doesn't do this already.

@GeraldHost
Copy link

@RichardLindhout any thoughts on this?

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

3 participants