diff --git a/packages/yew/src/context.rs b/packages/yew/src/context.rs index 28cbdfd0bde..d7596543f90 100644 --- a/packages/yew/src/context.rs +++ b/packages/yew/src/context.rs @@ -1,8 +1,7 @@ //! This module defines the `ContextProvider` component. -use crate::html::Context; use crate::html::Scope; -use crate::{html, Callback, Children, Component, Html, Properties, ShouldRender}; +use crate::{html, Callback, Children, Component, Context, Html, Properties}; use slab::Slab; use std::cell::RefCell; @@ -23,6 +22,7 @@ pub struct ContextProviderProps { #[derive(Debug)] pub struct ContextProvider { context: T, + children: Children, consumers: RefCell>>, } @@ -81,22 +81,32 @@ impl Component for ContextProvider { type Properties = ContextProviderProps; fn create(ctx: &Context) -> Self { + let props = ctx.props(); Self { - context: ctx.props().context.clone(), + children: props.children.clone(), + context: props.context.clone(), consumers: RefCell::new(Slab::new()), } } - fn update(&mut self, _ctx: &Context, _msg: Self::Message) -> ShouldRender { - true - } + fn changed(&mut self, ctx: &Context) -> bool { + let props = ctx.props(); + let should_render = if self.children == props.children { + false + } else { + self.children = props.children.clone(); + true + }; + + if self.context != props.context { + self.context = props.context.clone(); + self.notify_consumers(); + } - fn changed(&mut self, _ctx: &Context) -> bool { - self.notify_consumers(); - true + should_render } - fn view(&self, ctx: &Context) -> Html { - html! { <>{ ctx.props().children.clone() } } + fn view(&self, _ctx: &Context) -> Html { + html! { <>{ self.children.clone() } } } } diff --git a/packages/yew/tests/use_context.rs b/packages/yew/tests/use_context.rs index cf2f96dfbad..c00bf243deb 100644 --- a/packages/yew/tests/use_context.rs +++ b/packages/yew/tests/use_context.rs @@ -283,15 +283,14 @@ fn use_context_update_works() { assert_eq!(obtain_result_by_id("test-0"), "total: 4"); // 1 initial + 2 context update - // TODO Fix these - // assert_eq!( - // obtain_result_by_id("test-1"), - // "current: hello world!, total: 3" - // ); + assert_eq!( + obtain_result_by_id("test-1"), + "current: hello world!, total: 3" + ); // 1 initial + 1 context update + 1 magic update + 1 context update - // assert_eq!( - // obtain_result_by_id("test-2"), - // "current: hello world!, total: 4" - // ); + assert_eq!( + obtain_result_by_id("test-2"), + "current: hello world!, total: 4" + ); }