feat: context api overhaul

This commit is contained in:
Jonathan Kelley committed 2022-12-02 16:24:49 -08:00
1 parent 11a45c8ef0
commit a61daf220d
44 files changed
+812 -621

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
use dioxus_core::ScopeState;
/// Consume some context in the tree
pub fn use_context<T: 'static>(cx: &ScopeState) -> Option<&T> {
match *cx.use_hook(|| cx.consume_context::<T>().map(|t| t as *const T)) {
Some(res) => Some(unsafe { &*res }),
None => None,
}
}
/// Provide some context via the tree and return a reference to it
///
/// Once the context has been provided, it is immutable. Mutations should be done via interior mutability.
pub fn use_context_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> &T {
let ptr = *cx.use_hook(|| cx.provide_context(f()) as *const T);
unsafe { &*ptr }
}