Skip to content
\n

What's important here is that the on_commit callback is only ever used from update; it has no effect on view as it is not attached to any descendant element directly. My question is, would it be safe to custom-impl PartialEq for the props of this component to exclude on_commit from it?

\n

My concern is that I don't know if Yew will skip updating entirely if two old props and new props compare equal, or if it will attach the new props to the context but skip calling view (so the next time there is a call to update, update will have the most recent props.

\n

I could of course implement custom comparison logic in changed, but that would presumably duplicate work that Yew already does; I need to re-draw any time any property besides on_commit changes, so I would have to compare all of the fields besides on_commit, which Yew already does before calling changed.

\n

So I just want to know if Yew guarantees that a struct component's Context includes the most up to date properties, even if those properties compared equal to the old properties according to PartialEq.

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

Yew compares with the existing props and only replaces if they compare unequal. In this case, changed is then called to figure out if the component needs to re-render. Note that this callback also gives you access to the old props. So the correct way to avoid a re-render is to implement changed here.

\n

Small comment about the concern about duplicating work: The relevant code section is monomorphized per component, so I would think that the compiler is smart enough to inline changed in most cases and remove the duplicate checks in a lot of cases.

\n

Relevant code:

\n
\n

\n yew/packages/yew/src/html/component/lifecycle.rs\n

\n

\n Lines 211 to 216\n in\n 2e464ed\n

\n
\n
\n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n
if self.context.props != props {
let old_props = std::mem::replace(&mut self.context.props, props);
self.component.changed(&self.context, &old_props)
} else {
false
}
\n
\n
\n

","upvoteCount":1,"url":"https://github.com/yewstack/yew/discussions/3739#discussioncomment-10811687"}}}
Discussion options

You must be logged in to vote

Yew compares with the existing props and only replaces if they compare unequal. In this case, changed is then called to figure out if the component needs to re-render. Note that this callback also gives you access to the old props. So the correct way to avoid a re-render is to implement changed here.

Small comment about the concern about duplicating work: The relevant code section is monomorphized per component, so I would think that the compiler is smart enough to inline changed in most cases and remove the duplicate checks in a lot of cases.

Relevant code:

if self.context.props != props {
let o…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@zstewar1
Comment options

@WorldSEnder
Comment options

Answer selected by zstewar1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants