Skip to content

Commit

Permalink
forgot to commit that oops
Browse files Browse the repository at this point in the history
  • Loading branch information
its-the-shrimp committed Sep 23, 2023
1 parent a65de04 commit 2fb5d4c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/yew/src/functional/hooks/use_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::functional::{Hook, HookContext};
/// is returned. A component which calls `use_context` will re-render when the data of the context
/// changes.
///
/// More information about contexts and how to define and consume them can be found on [Yew Docs](https://yew.rs).
/// More information about contexts and how to define and consume them can be found on [Yew Docs](https://yew.rs/docs/concepts/contexts).
///
/// # Example
///
Expand Down
46 changes: 34 additions & 12 deletions website/docs/concepts/contexts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,57 @@ A context provider is required to consume the context. `ContextProvider<T>`, whe
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:

```rust
use std::rc::Rc;
use yew::prelude::*;


/// App theme
#[derive(Clone, Debug, PartialEq)]
struct Theme {
foreground: String,
background: String,
}

/// Main component
#[function_component]
fn NavButton() -> Html {
let theme = use_context::<Theme>();
pub fn App() -> Html {
let ctx = use_state(|| Theme {
foreground: "#000000".to_owned(),
background: "#eeeeee".to_owned(),
});

html! {
// use theme
// `ctx` is type `Rc<UseStateHandle<Theme>>` while we need `Theme`
// so we deref it.
// It derefs to `&Theme`, hence the clone
<ContextProvider<Theme> context={(*ctx).clone()}>
// Every child here and their children will have access to this context.
<Toolbar />
</ContextProvider<Theme>>
}
}

/// The toolbar.
/// This component has access to the context
#[function_component]
fn App() -> Html {
let theme = use_memo((), |_| Theme {
foreground: "yellow".to_owned(),
background: "pink".to_owned(),
});
pub fn Toolbar() -> Html {
html! {
<div>
<ThemedButton />
</div>
}
}

/// Button placed in `Toolbar`.
/// As this component is a child of `ThemeContextProvider` in the component tree, it also has access
/// to the context.
#[function_component]
pub fn ThemedButton() -> Html {
let theme = use_context::<Theme>().expect("no ctx found");

html! {
<ContextProvider<Rc<Theme>> context={theme}>
<NavButton />
</ContextProvider<Rc<Theme>>>
<button style={format!("background: {}; color: {};", theme.background, theme.foreground)}>
{ "Click me!" }
</button>
}
}
```
Expand Down

0 comments on commit 2fb5d4c

Please sign in to comment.