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

Make fn update() re-render the component by default #2786

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 40 additions & 1 deletion packages/yew/src/html/component/mod.rs
Expand Up @@ -181,14 +181,18 @@ pub trait Component: Sized + 'static {
/// to update their state and (optionally) re-render themselves.
///
/// Returned bool indicates whether to render this Component after update.
///
/// By default, this function will return true and thus make the component re-render.
#[allow(unused_variables)]
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
false
true
}

/// Called when properties passed to the component change
///
/// Returned bool indicates whether to render this Component after changed.
///
/// By default, this function will return true and thus make the component re-render.
#[allow(unused_variables)]
fn changed(&mut self, ctx: &Context<Self>) -> bool {
true
Expand Down Expand Up @@ -262,3 +266,38 @@ where
Component::prepare_state(self)
}
}

#[cfg(test)]
mod tests {
use super::*;

struct MyCustomComponent;

impl Component for MyCustomComponent {
type Message = ();
type Properties = ();

fn create(_ctx: &Context<Self>) -> Self {
Self
}

fn view(&self, _ctx: &Context<Self>) -> Html {
Default::default()
}
}

#[test]
fn make_sure_component_update_and_changed_rerender() {
let mut comp = MyCustomComponent;
let ctx = Context {
scope: Scope::new(None),
props: Rc::new(()),
#[cfg(feature = "hydration")]
creation_mode: crate::html::RenderMode::Hydration,
#[cfg(feature = "hydration")]
prepared_state: None,
};
assert!(Component::update(&mut comp, &ctx, ()));
assert!(Component::changed(&mut comp, &ctx));
}
}