Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion packages/yew/src/html/component/mod.rs
Original file line number Diff line number Diff line change
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,34 @@ 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(()),
};
assert!(comp.update(&ctx, ()));
assert!(comp.changed(&ctx));
}
}