Skip to content

Commit 5bf16f5

Browse files
committed
fix context
1 parent 712636b commit 5bf16f5

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

packages/yew/src/context.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! This module defines the `ContextProvider` component.
22
3-
use crate::html::Context;
43
use crate::html::Scope;
5-
use crate::{html, Callback, Children, Component, Html, Properties, ShouldRender};
4+
use crate::{html, Callback, Children, Component, Context, Html, Properties};
65
use slab::Slab;
76
use std::cell::RefCell;
87

@@ -23,6 +22,7 @@ pub struct ContextProviderProps<T: Clone + PartialEq> {
2322
#[derive(Debug)]
2423
pub struct ContextProvider<T: Clone + PartialEq + 'static> {
2524
context: T,
25+
children: Children,
2626
consumers: RefCell<Slab<Callback<T>>>,
2727
}
2828

@@ -81,22 +81,32 @@ impl<T: Clone + PartialEq + 'static> Component for ContextProvider<T> {
8181
type Properties = ContextProviderProps<T>;
8282

8383
fn create(ctx: &Context<Self>) -> Self {
84+
let props = ctx.props();
8485
Self {
85-
context: ctx.props().context.clone(),
86+
children: props.children.clone(),
87+
context: props.context.clone(),
8688
consumers: RefCell::new(Slab::new()),
8789
}
8890
}
8991

90-
fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> ShouldRender {
91-
true
92-
}
92+
fn changed(&mut self, ctx: &Context<Self>) -> bool {
93+
let props = ctx.props();
94+
let should_render = if self.children == props.children {
95+
false
96+
} else {
97+
self.children = props.children.clone();
98+
true
99+
};
100+
101+
if self.context != props.context {
102+
self.context = props.context.clone();
103+
self.notify_consumers();
104+
}
93105

94-
fn changed(&mut self, _ctx: &Context<Self>) -> bool {
95-
self.notify_consumers();
96-
true
106+
should_render
97107
}
98108

99-
fn view(&self, ctx: &Context<Self>) -> Html {
100-
html! { <>{ ctx.props().children.clone() }</> }
109+
fn view(&self, _ctx: &Context<Self>) -> Html {
110+
html! { <>{ self.children.clone() }</> }
101111
}
102112
}

packages/yew/tests/use_context.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,14 @@ fn use_context_update_works() {
283283
assert_eq!(obtain_result_by_id("test-0"), "total: 4");
284284

285285
// 1 initial + 2 context update
286-
// TODO Fix these
287-
// assert_eq!(
288-
// obtain_result_by_id("test-1"),
289-
// "current: hello world!, total: 3"
290-
// );
286+
assert_eq!(
287+
obtain_result_by_id("test-1"),
288+
"current: hello world!, total: 3"
289+
);
291290

292291
// 1 initial + 1 context update + 1 magic update + 1 context update
293-
// assert_eq!(
294-
// obtain_result_by_id("test-2"),
295-
// "current: hello world!, total: 4"
296-
// );
292+
assert_eq!(
293+
obtain_result_by_id("test-2"),
294+
"current: hello world!, total: 4"
295+
);
297296
}

0 commit comments

Comments
 (0)