diff --git a/packages/yew/src/html/component/mod.rs b/packages/yew/src/html/component/mod.rs index 990f980666f..9f212c0527b 100644 --- a/packages/yew/src/html/component/mod.rs +++ b/packages/yew/src/html/component/mod.rs @@ -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, 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) -> bool { true @@ -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 + } + + fn view(&self, _ctx: &Context) -> 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)); + } +}