Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Component::update() should not return ShouldRender but auto-detect state changes to schedule a re-render #435

Closed
Boscop opened this issue Nov 18, 2018 · 3 comments
Labels
A-yew Area: The main yew crate proposal

Comments

@Boscop
Copy link

Boscop commented Nov 18, 2018

@deniskolodin
This issue is a suggestion for Yew's design.

If we go with the solution suggested here #350 (comment) we need a way to re-render a child's DOM when its state changed after calling child.update(ChildMsg::SomeAction) from the parent.
Currently, Component::update() returns a bool (ShouldRender), but it would be ignored when update is directly called by the parent. We ONLY want to re-render the child's state when child.update() changed its state, we don't want to return true from the parent's update() call, because the rest of the parent's DOM should NOT be re-rendered.
If each component could auto-detect when its state has changed from before and after its update() call, it could automatically schedule its own re-rendering.
In Halogen this is done by using a State monad that "manages" each component's state. This allows detecting when the component's state has been modified.
We don't have to introduce monads to get the same result, just some wrapper struct that derefs to its contents, and sets a dirty flag to true when it's deref_mut() is called (which does not mean that the state has necessarily changed (it might just be that it was mutably borrowed but not modified) but it's a good indicator that a DOM re-render might be necessary, i.e. a necessary but not sufficient condition).
If we mandate that the component's real state type has to be PartialEq, the Yew runtime can automatically compare the "before" and "after" states (only necessary if the dirty flag is true though) and then request a re-render of the component's DOM.

What do you think? :)

@jstarry
Copy link
Member

jstarry commented Feb 3, 2020

I'm open to this but I think we would still want to give developers the ability to fine-tune re-renders.. something like https://reactjs.org/docs/react-component.html#shouldcomponentupdate

@Boscop
Copy link
Author

Boscop commented Feb 10, 2020

@jstarry Yes, that would make sense. But also having something like PureComponent that does the comparison:

Consider using the built-in PureComponent instead of writing shouldComponentUpdate() by hand. PureComponent performs a shallow comparison of props and state, and reduces the chance that you’ll skip a necessary update.

@jstarry
Copy link
Member

jstarry commented Feb 10, 2020

Yeah, once we have a more sophisticated diffing algorithm we will definitely want to introduce PureComponents.

@mc1098 mc1098 added the A-yew Area: The main yew crate label Sep 20, 2021
@yewstack yewstack locked and limited conversation to collaborators Sep 26, 2021
@mc1098 mc1098 closed this as completed Sep 26, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
A-yew Area: The main yew crate proposal
Projects
None yet
Development

No branches or pull requests

3 participants