0.7 Provider component
This commit is contained in:
parent
4323e30133
commit
76506c03e1
|
@ -239,6 +239,7 @@ pub use typed_builder_macro;
|
|||
mod into_view;
|
||||
pub use into_view::IntoView;
|
||||
pub use leptos_dom;
|
||||
mod provider;
|
||||
pub use tachys;
|
||||
/// Tools to mount an application to the DOM, or to hydrate it from server-rendered HTML.
|
||||
pub mod mount;
|
||||
|
@ -250,6 +251,7 @@ pub use reactive_graph;
|
|||
|
||||
/// Provide and access data along the reactive graph, sharing data without directly passing arguments.
|
||||
pub mod context {
|
||||
pub use crate::provider::*;
|
||||
pub use reactive_graph::owner::{provide_context, use_context};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use leptos::*;
|
||||
use crate::{children::TypedChildren, component, IntoView};
|
||||
use reactive_graph::owner::{provide_context, Owner};
|
||||
use tachys::reactive_graph::OwnedView;
|
||||
|
||||
#[component]
|
||||
/// Uses the context API to [`provide_context`] to its children and descendants,
|
||||
|
@ -7,7 +9,7 @@ use leptos::*;
|
|||
/// This prevents issues related to “context shadowing.”
|
||||
///
|
||||
/// ```rust
|
||||
/// # use leptos::*;
|
||||
/// # use leptos::prelude::*;
|
||||
/// #[component]
|
||||
/// pub fn App() -> impl IntoView {
|
||||
/// // each Provider will only provide the value to its children
|
||||
|
@ -25,16 +27,22 @@ use leptos::*;
|
|||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn Provider<T>(
|
||||
pub fn Provider<T, Chil>(
|
||||
/// The value to be provided via context.
|
||||
value: T,
|
||||
children: Children,
|
||||
children: TypedChildren<Chil>,
|
||||
) -> impl IntoView
|
||||
where
|
||||
T: Clone + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
Chil: IntoView,
|
||||
{
|
||||
run_as_child(move || {
|
||||
let owner = Owner::current()
|
||||
.expect("no current reactive Owner found")
|
||||
.child();
|
||||
let children = children.into_inner();
|
||||
let children = owner.with(|| {
|
||||
provide_context(value);
|
||||
children()
|
||||
})
|
||||
});
|
||||
OwnedView::new_with_owner(children, owner)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue