From 0d66a5d0c44a3707b677406b0abc968b8bf03419 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 20 Jul 2022 14:50:56 +0100 Subject: [PATCH 1/4] Make fn update() re-render the component by default --- packages/yew/src/html/component/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/yew/src/html/component/mod.rs b/packages/yew/src/html/component/mod.rs index 6c405d4ee3b..f603f9f4562 100644 --- a/packages/yew/src/html/component/mod.rs +++ b/packages/yew/src/html/component/mod.rs @@ -60,14 +60,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 From 4187f1e3a67ec71261db484a3bbac011c509bfb4 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 20 Jul 2022 15:11:04 +0100 Subject: [PATCH 2/4] Add test --- packages/yew/src/html/component/mod.rs | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/yew/src/html/component/mod.rs b/packages/yew/src/html/component/mod.rs index f603f9f4562..b90ef78ca9a 100644 --- a/packages/yew/src/html/component/mod.rs +++ b/packages/yew/src/html/component/mod.rs @@ -99,3 +99,34 @@ pub trait Component: Sized + 'static { #[allow(unused_variables)] fn destroy(&mut self, ctx: &Context) {} } + +#[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(()), + }; + assert!(comp.update(&ctx, ())); + assert!(comp.changed(&ctx)); + } +} From 3d1b49eb51af4e27c205da388bc31528a57eb50f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 20 Jul 2022 15:22:39 +0100 Subject: [PATCH 3/4] WIP --- packages/yew/src/html/component/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/yew/src/html/component/mod.rs b/packages/yew/src/html/component/mod.rs index 34a3f0d4b6f..63c3bd23435 100644 --- a/packages/yew/src/html/component/mod.rs +++ b/packages/yew/src/html/component/mod.rs @@ -292,8 +292,12 @@ mod tests { let ctx = Context { scope: Scope::new(None), props: Rc::new(()), + #[cfg(feature = "hydration")] + creation_mode: crate::html::RenderMode, + #[cfg(feature = "hydration")] + prepared_state: None, }; - assert!(comp.update(&ctx, ())); - assert!(comp.changed(&ctx)); + assert!(Component::update(&mut comp, &ctx, ())); + assert!(Component::changed(&mut comp, &ctx)); } } From 0f50ca97cef866c9a4e4581e001db120ec4c3dd4 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 20 Jul 2022 15:24:56 +0100 Subject: [PATCH 4/4] WIP --- packages/yew/src/html/component/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/yew/src/html/component/mod.rs b/packages/yew/src/html/component/mod.rs index 63c3bd23435..9f212c0527b 100644 --- a/packages/yew/src/html/component/mod.rs +++ b/packages/yew/src/html/component/mod.rs @@ -293,7 +293,7 @@ mod tests { scope: Scope::new(None), props: Rc::new(()), #[cfg(feature = "hydration")] - creation_mode: crate::html::RenderMode, + creation_mode: crate::html::RenderMode::Hydration, #[cfg(feature = "hydration")] prepared_state: None, };