-
-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use leptos::*; | ||
|
||
#[component] | ||
/// Uses the context API to [`provide_context`] to its children and descendants, | ||
/// without overwriting any contexts of the same type in its own reactive scope. | ||
/// | ||
/// This prevents issues related to “context shadowing.” | ||
/// | ||
/// ```rust | ||
/// # use leptos::*; | ||
/// #[component] | ||
/// pub fn App() -> impl IntoView { | ||
/// // each Provider will only provide the value to its children | ||
/// view! { | ||
/// <Provider value=1u8> | ||
/// // correctly gets 1 from context | ||
/// {use_context::<u8>().unwrap_or(0)} | ||
/// </Provider> | ||
/// <Provider value=2u8> | ||
/// // correctly gets 2 from context | ||
/// {use_context::<u8>().unwrap_or(0)} | ||
/// </Provider> | ||
/// // does not find any u8 in context | ||
/// {use_context::<u8>().unwrap_or(0)} | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn Provider<T>( | ||
/// The value to be provided via context. | ||
value: T, | ||
children: Children, | ||
) -> impl IntoView | ||
where | ||
T: Clone + 'static, | ||
{ | ||
run_as_child(move || { | ||
provide_context(value); | ||
children() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters