Skip to content

Commit

Permalink
Optimize the default change implementation (#690)
Browse files Browse the repository at this point in the history
* Optimize the default `change` implementation

Return `false` if `Self::Properties` has a value of `()`.

* Use TypeId for checking Properties == ()
  • Loading branch information
kellytk authored and jstarry committed Oct 13, 2019
1 parent 058b85f commit 23c6c12
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use scope::{NodeCell, Scope};

use crate::callback::Callback;
use crate::virtual_dom::{VChild, VList, VNode};
use std::any::TypeId;
use std::fmt;

/// This type indicates that component should be rendered again.
Expand All @@ -37,10 +38,10 @@ pub trait Component: Sized + 'static {
/// Called when the component's parent component re-renders and the
/// component's place in the DOM tree remains unchanged. If the component's
/// place in the DOM tree changes, calling this method is unnecessary as the
/// component is recreated from scratch. It defaults
/// to true if not implemented.
fn change(&mut self, _: Self::Properties) -> ShouldRender {
true
/// component is recreated from scratch. It defaults to true if not implemented
/// and Self::Properties is not the unit type `()`.
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
TypeId::of::<Self::Properties>() != TypeId::of::<()>()
}
/// Called by rendering loop.
fn view(&self) -> Html<Self>;
Expand Down

0 comments on commit 23c6c12

Please sign in to comment.