Skip to content

Commit a2786b1

Browse files
Improve Context API docs (#3409)
* improved context API docs, added example of struct component context producer * forgot to commit that oops
1 parent 954b0ec commit a2786b1

File tree

7 files changed

+66
-18
lines changed

7 files changed

+66
-18
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/contexts/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ edition = "2021"
55
license = "MIT OR Apache-2.0"
66

77
[dependencies]
8-
serde = { version = "1.0", features = ["derive"] }
98
yew = { path = "../../packages/yew", features = ["csr"] }
109
yew-agent = { path = "../../packages/yew-agent" }

examples/contexts/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod msg_ctx;
22
mod producer;
3+
mod struct_component_producer;
34
mod struct_component_subscriber;
45
mod subscriber;
56

67
use msg_ctx::MessageProvider;
78
use producer::Producer;
9+
use struct_component_producer::StructComponentProducer;
810
use struct_component_subscriber::StructComponentSubscriber;
911
use subscriber::Subscriber;
1012
use yew::prelude::*;
@@ -14,6 +16,7 @@ pub fn App() -> Html {
1416
html! {
1517
<MessageProvider>
1618
<Producer />
19+
<StructComponentProducer />
1720
<Subscriber />
1821
<StructComponentSubscriber />
1922
</MessageProvider>

examples/contexts/src/producer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ use super::msg_ctx::MessageContext;
66
pub fn Producer() -> Html {
77
let msg_ctx = use_context::<MessageContext>().unwrap();
88

9-
let onclick = Callback::from(move |_| msg_ctx.dispatch("Message Received.".to_string()));
10-
119
html! {
12-
<button {onclick}>
10+
<button onclick={move |_| msg_ctx.dispatch("Message Received.".to_string())}>
1311
{"PRESS ME"}
1412
</button>
1513
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use yew::prelude::*;
2+
3+
use super::msg_ctx::MessageContext;
4+
5+
pub struct StructComponentProducer;
6+
7+
impl Component for StructComponentProducer {
8+
type Message = ();
9+
type Properties = ();
10+
11+
fn create(_ctx: &Context<Self>) -> Self {
12+
Self
13+
}
14+
15+
fn view(&self, ctx: &Context<Self>) -> Html {
16+
let (msg_ctx, _) = ctx
17+
.link()
18+
.context::<MessageContext>(Callback::noop())
19+
.expect("No Message Context Provided");
20+
21+
html! {
22+
<button onclick={move |_| msg_ctx.dispatch("Other message received.".to_owned())}>
23+
{"OR ME"}
24+
</button>
25+
}
26+
}
27+
}

packages/yew/src/functional/hooks/use_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::functional::{Hook, HookContext};
1111
/// is returned. A component which calls `use_context` will re-render when the data of the context
1212
/// changes.
1313
///
14-
/// More information about contexts and how to define and consume them can be found on [Yew Docs](https://yew.rs).
14+
/// More information about contexts and how to define and consume them can be found on [Yew Docs](https://yew.rs/docs/concepts/contexts).
1515
///
1616
/// # Example
1717
///

website/docs/concepts/contexts.mdx

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,57 @@ A context provider is required to consume the context. `ContextProvider<T>`, whe
9696
The children are re-rendered when the context changes. A struct is used to define what data is to be passed. The `ContextProvider` can be used as:
9797

9898
```rust
99-
use std::rc::Rc;
10099
use yew::prelude::*;
101100

101+
102+
/// App theme
102103
#[derive(Clone, Debug, PartialEq)]
103104
struct Theme {
104105
foreground: String,
105106
background: String,
106107
}
107108

109+
/// Main component
108110
#[function_component]
109-
fn NavButton() -> Html {
110-
let theme = use_context::<Theme>();
111+
pub fn App() -> Html {
112+
let ctx = use_state(|| Theme {
113+
foreground: "#000000".to_owned(),
114+
background: "#eeeeee".to_owned(),
115+
});
111116

112117
html! {
113-
// use theme
118+
// `ctx` is type `Rc<UseStateHandle<Theme>>` while we need `Theme`
119+
// so we deref it.
120+
// It derefs to `&Theme`, hence the clone
121+
<ContextProvider<Theme> context={(*ctx).clone()}>
122+
// Every child here and their children will have access to this context.
123+
<Toolbar />
124+
</ContextProvider<Theme>>
114125
}
115126
}
116127

128+
/// The toolbar.
129+
/// This component has access to the context
117130
#[function_component]
118-
fn App() -> Html {
119-
let theme = use_memo((), |_| Theme {
120-
foreground: "yellow".to_owned(),
121-
background: "pink".to_owned(),
122-
});
131+
pub fn Toolbar() -> Html {
132+
html! {
133+
<div>
134+
<ThemedButton />
135+
</div>
136+
}
137+
}
138+
139+
/// Button placed in `Toolbar`.
140+
/// As this component is a child of `ThemeContextProvider` in the component tree, it also has access
141+
/// to the context.
142+
#[function_component]
143+
pub fn ThemedButton() -> Html {
144+
let theme = use_context::<Theme>().expect("no ctx found");
123145

124146
html! {
125-
<ContextProvider<Rc<Theme>> context={theme}>
126-
<NavButton />
127-
</ContextProvider<Rc<Theme>>>
147+
<button style={format!("background: {}; color: {};", theme.background, theme.foreground)}>
148+
{ "Click me!" }
149+
</button>
128150
}
129151
}
130152
```

0 commit comments

Comments
 (0)