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

The component doesn't redraw, but the component function is still executed #97

Closed
runenielsen opened this issue Nov 9, 2022 · 8 comments

Comments

@runenielsen
Copy link

I have tested your minimal example, and when I insert console.log('Counter') in the Counter function and console.log('TextBox') in the TextBox function, all the sub-components log, when I click on a counter button or change a text, even if only the component type where I made the change is actually redrawn (the random number changes).

So the component functions for TextBox are still executed, when I update the Counter and vice versa, even if the result for some reason isn't used by React to actually redraw the Textbox components.

I assume this isn't the intention...?

@dai-shi
Copy link
Owner

dai-shi commented Nov 9, 2022

Can you put console.log in useEffect and see how it goes? If the result is somewhat expected, it's intentional behavior.

@runenielsen
Copy link
Author

When I put console.log in useEffect, it works as I would expect (only the component I change logs anything). I'm not sure I understand, why this is intentional though. I would definitely prefer the unrelated components to not be called at all, not sort of a halfway render :-)

@dai-shi
Copy link
Owner

dai-shi commented Nov 9, 2022

It's required to avoid so called stale props issue. You don't want some mismatch between props and context value.
If it's not the issue for your case, what you want should be possible.

@runenielsen
Copy link
Author

Ok, thanks.

@willdspd
Copy link

Seconding this. I just spent quite a long time trying to figure out why my components were re-rendering when using this library because I was using console.log. In my opinion, if possible, the default functionality should 100% be to not call unrelated components. For one, it's confusing when testing the lib, two, because it feels less clean somehow. @dai-shi Fantastic library aside from this. Thank you for all your work in the react state space!

@dai-shi
Copy link
Owner

dai-shi commented Jan 14, 2023

If we give up concurrent rendering support, it's possible.
But, that's the whole motivation of developing this library. https://github.com/dai-shi/will-this-react-global-state-work-in-concurrent-rendering

Without such capability, it's pretty easier to implement an alternative library.

FYI, the unexpected behavior is just how useReducer works. https://twitter.com/dai_shi/status/1534170089981100032

@willdspd
Copy link

willdspd commented Jan 14, 2023

I haven't played around much with concurrent mode so will reserve an opinion on this. It's a tiny issue with an overall fantastic library, so again, thank you!

@dai-shi
Copy link
Owner

dai-shi commented Jan 29, 2023

fwiw, if you have a stable selector, the behavior might be improved. #100 (comment)

Closing as answered.

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