diff --git a/yew-router/examples/guide/src/guide.rs b/yew-router/examples/guide/src/guide.rs index 193176d73a9..9545790fb77 100644 --- a/yew-router/examples/guide/src/guide.rs +++ b/yew-router/examples/guide/src/guide.rs @@ -34,18 +34,21 @@ impl Component for Guide { } } - fn mounted(&mut self) -> ShouldRender { + fn rendered(&mut self, _first_render: bool) { self.router_agent.send(GetCurrentRoute); - false } fn update(&mut self, msg: Self::Message) -> bool { match msg { Msg::UpdateRoute(route) => { - self.route = Some(route); + let new_route = Some(route); + if self.route != new_route { + self.route = new_route; + return true; + } } } - true + false } fn change(&mut self, _: Self::Properties) -> bool { diff --git a/yew-router/examples/guide/src/markdown_window.rs b/yew-router/examples/guide/src/markdown_window.rs index 16132bf0c37..337c76fcda4 100644 --- a/yew-router/examples/guide/src/markdown_window.rs +++ b/yew-router/examples/guide/src/markdown_window.rs @@ -41,10 +41,6 @@ impl Component for MarkdownWindow { } } - fn mounted(&mut self) -> ShouldRender { - false - } - fn update(&mut self, msg: Self::Message) -> bool { match msg { Msg::MarkdownArrived(md) => { diff --git a/yew-router/src/router.rs b/yew-router/src/router.rs index 4e0a349986e..169d9069ae7 100644 --- a/yew-router/src/router.rs +++ b/yew-router/src/router.rs @@ -185,9 +185,8 @@ where } } - fn mounted(&mut self) -> ShouldRender { + fn rendered(&mut self, _first_render: bool) { self.router_agent.send(RouteRequest::GetCurrentRoute); - false } fn update(&mut self, msg: Self::Message) -> ShouldRender { diff --git a/yew/src/html/scope.rs b/yew/src/html/scope.rs index d2234d9308a..f286249d268 100644 --- a/yew/src/html/scope.rs +++ b/yew/src/html/scope.rs @@ -174,7 +174,7 @@ impl Scope { enum ComponentState { Empty, Ready(ReadyState), - Created((bool, CreatedState)), + Created(CreatedState), Processing, Destroyed, } @@ -203,6 +203,7 @@ struct ReadyState { impl ReadyState { fn create(self) -> CreatedState { CreatedState { + rendered: false, component: COMP::create(self.props, self.scope), element: self.element, last_frame: self.ancestor, @@ -212,6 +213,7 @@ impl ReadyState { } struct CreatedState { + rendered: bool, element: Element, component: COMP, last_frame: Option, @@ -221,6 +223,7 @@ struct CreatedState { impl CreatedState { /// Called after a component and all of its children have been rendered. fn rendered(mut self, first_render: bool) -> Self { + self.rendered = true; self.component.rendered(first_render); self } @@ -256,14 +259,10 @@ where fn run(self: Box) { let current_state = self.shared_state.replace(ComponentState::Processing); self.shared_state.replace(match current_state { - ComponentState::Created((needs_render, state)) => { - if needs_render { - ComponentState::Created((false, state.rendered(self.first_render))) - } else { - ComponentState::Created((needs_render, state)) - } + ComponentState::Created(s) if !s.rendered => { + ComponentState::Created(s.rendered(self.first_render)) } - ComponentState::Destroyed => current_state, + ComponentState::Destroyed | ComponentState::Created(_) => current_state, ComponentState::Empty | ComponentState::Processing | ComponentState::Ready(_) => { panic!("unexpected component state: {}", current_state); } @@ -285,9 +284,7 @@ where fn run(self: Box) { let current_state = self.shared_state.replace(ComponentState::Processing); self.shared_state.replace(match current_state { - ComponentState::Ready(state) => { - ComponentState::Created((true, state.create().update())) - } + ComponentState::Ready(s) => ComponentState::Created(s.create().update()), ComponentState::Created(_) | ComponentState::Destroyed => current_state, ComponentState::Empty | ComponentState::Processing => { panic!("unexpected component state: {}", current_state); @@ -309,7 +306,7 @@ where { fn run(self: Box) { match self.shared_state.replace(ComponentState::Destroyed) { - ComponentState::Created((_, mut this)) => { + ComponentState::Created(mut this) => { this.component.destroy(); if let Some(last_frame) = &mut this.last_frame { last_frame.detach(&this.element); @@ -341,7 +338,7 @@ where fn run(self: Box) { let current_state = self.shared_state.replace(ComponentState::Processing); self.shared_state.replace(match current_state { - ComponentState::Created((needs_render, mut this)) => { + ComponentState::Created(mut this) => { let should_update = match self.update { ComponentUpdate::Message(message) => this.component.update(message), ComponentUpdate::MessageBatch(messages) => messages @@ -354,8 +351,13 @@ where this.component.change(props) } }; - let next_state = if should_update { this.update() } else { this }; - ComponentState::Created((!needs_render && !should_update, next_state)) + let next_state = if should_update { + this.rendered = false; + this.update() + } else { + this + }; + ComponentState::Created(next_state) } ComponentState::Destroyed => current_state, ComponentState::Processing | ComponentState::Ready(_) | ComponentState::Empty => {