diff --git a/docs/guide/examples/hooks_composed.rs b/docs/guide/examples/hooks_composed.rs index f14c1a323..5b1f5ebfe 100644 --- a/docs/guide/examples/hooks_composed.rs +++ b/docs/guide/examples/hooks_composed.rs @@ -8,6 +8,6 @@ struct AppSettings {} // ANCHOR: wrap_context fn use_settings(cx: &ScopeState) -> UseSharedState { - use_context::(cx).expect("App settings not provided") + use_shared_state::(cx).expect("App settings not provided") } // ANCHOR_END: wrap_context diff --git a/docs/guide/examples/meme_editor_dark_mode.rs b/docs/guide/examples/meme_editor_dark_mode.rs index 27ee50f37..e394a4a7f 100644 --- a/docs/guide/examples/meme_editor_dark_mode.rs +++ b/docs/guide/examples/meme_editor_dark_mode.rs @@ -1,7 +1,6 @@ // ANCHOR: all #![allow(non_snake_case)] -use dioxus::events::FormData; use dioxus::prelude::*; fn main() { @@ -14,7 +13,7 @@ struct DarkMode(bool); pub fn App(cx: Scope) -> Element { // ANCHOR: context_provider - use_context_provider(cx, || DarkMode(false)); + use_shared_state_provider(cx, || DarkMode(false)); // ANCHOR_END: context_provider let is_dark_mode = use_is_dark_mode(cx); @@ -37,7 +36,7 @@ pub fn App(cx: Scope) -> Element { pub fn use_is_dark_mode(cx: &ScopeState) -> bool { // ANCHOR: use_context - let dark_mode_context = use_context::(cx); + let dark_mode_context = use_shared_state::(cx); // ANCHOR_END: use_context dark_mode_context @@ -47,7 +46,7 @@ pub fn use_is_dark_mode(cx: &ScopeState) -> bool { // ANCHOR: toggle pub fn DarkModeToggle(cx: Scope) -> Element { - let dark_mode = use_context::(cx)?; + let dark_mode = use_shared_state::(cx).unwrap(); let style = if dark_mode.read().0 { "color:white" diff --git a/packages/hooks/src/lib.rs b/packages/hooks/src/lib.rs index ceafc218d..583eccb37 100644 --- a/packages/hooks/src/lib.rs +++ b/packages/hooks/src/lib.rs @@ -30,8 +30,8 @@ pub use usestate::{use_state, UseState}; mod useref; pub use useref::*; -// mod use_shared_state; -// pub use use_shared_state::*; +mod use_shared_state; +pub use use_shared_state::*; mod usecoroutine; pub use usecoroutine::*; diff --git a/packages/hooks/src/use_shared_state.rs b/packages/hooks/src/use_shared_state.rs index ebb848143..a355ae42a 100644 --- a/packages/hooks/src/use_shared_state.rs +++ b/packages/hooks/src/use_shared_state.rs @@ -60,10 +60,10 @@ impl ProvidedStateInner { /// /// /// -pub fn use_context(cx: &ScopeState) -> Option> { +pub fn use_shared_state(cx: &ScopeState) -> Option> { let state = cx.use_hook(|| { let scope_id = cx.scope_id(); - let root = cx.consume_context::>().cloned(); + let root = cx.consume_context::>(); if let Some(root) = root.as_ref() { root.borrow_mut().consumers.insert(scope_id); @@ -165,14 +165,7 @@ where } /// Provide some state for components down the hierarchy to consume without having to drill props. -/// -/// -/// -/// -/// -/// -/// -pub fn use_context_provider(cx: &ScopeState, f: impl FnOnce() -> T) { +pub fn use_shared_state_provider(cx: &ScopeState, f: impl FnOnce() -> T) { cx.use_hook(|| { let state: ProvidedState = Rc::new(RefCell::new(ProvidedStateInner { value: Rc::new(RefCell::new(f())), diff --git a/packages/hooks/src/usecontext.rs b/packages/hooks/src/usecontext.rs index 74f7cea93..fdf363252 100644 --- a/packages/hooks/src/usecontext.rs +++ b/packages/hooks/src/usecontext.rs @@ -1,6 +1,8 @@ use dioxus_core::ScopeState; /// Consume some context in the tree, providing a sharable handle to the value +/// +/// Does not regenerate the value if the value is changed at the parent. pub fn use_context(cx: &ScopeState) -> Option<&T> { cx.use_hook(|| cx.consume_context::()).as_ref() } diff --git a/packages/hooks/src/usecoroutine.rs b/packages/hooks/src/usecoroutine.rs index f65d0b0eb..8c735bd80 100644 --- a/packages/hooks/src/usecoroutine.rs +++ b/packages/hooks/src/usecoroutine.rs @@ -2,8 +2,6 @@ use dioxus_core::{ScopeState, TaskId}; pub use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender}; use std::future::Future; -use crate::{use_context, use_context_provider}; - /// Maintain a handle over a future that can be paused, resumed, and canceled. /// /// This is an upgraded form of [`use_future`] with an integrated channel system. @@ -67,10 +65,10 @@ where G: FnOnce(UnboundedReceiver) -> F, F: Future + 'static, { - use_context_provider(cx, || { + cx.use_hook(|| { let (tx, rx) = futures_channel::mpsc::unbounded(); let task = cx.push_future(init(rx)); - Coroutine { tx, task } + cx.provide_context(Coroutine { tx, task }) }) } @@ -78,7 +76,8 @@ where /// /// See the docs for [`use_coroutine`] for more details. pub fn use_coroutine_handle(cx: &ScopeState) -> Option<&Coroutine> { - use_context::>(cx) + cx.use_hook(|| cx.consume_context::>()) + .as_ref() } pub struct Coroutine { diff --git a/packages/router/src/components/router.rs b/packages/router/src/components/router.rs index 14a0e14e7..54131a13a 100644 --- a/packages/router/src/components/router.rs +++ b/packages/router/src/components/router.rs @@ -39,15 +39,15 @@ pub struct RouterProps<'a> { /// Will fallback to HashRouter is BrowserRouter is not available, or through configuration. #[allow(non_snake_case)] pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element { - let svc = use_context_provider(cx, || { - RouterService::new( + let svc = cx.use_hook(|| { + cx.provide_context(RouterService::new( cx, RouterCfg { base_url: cx.props.base_url.map(|s| s.to_string()), active_class: cx.props.active_class.map(|s| s.to_string()), initial_url: cx.props.initial_url.clone(), }, - ) + )) }); // next time we run the rout_found will be filled diff --git a/packages/rsx/src/element.rs b/packages/rsx/src/element.rs index 55792cc5a..307059f9d 100644 --- a/packages/rsx/src/element.rs +++ b/packages/rsx/src/element.rs @@ -192,16 +192,16 @@ impl ToTokens for Element { #[derive(PartialEq, Eq, Clone, Debug, Hash)] pub enum ElementAttr { - /// attribute: "valuee {}" + /// `attribute: "value"` AttrText { name: Ident, value: IfmtInput }, - /// attribute: true, + /// `attribute: true` AttrExpression { name: Ident, value: Expr }, - /// "attribute": "value {}" + /// `"attribute": "value"` CustomAttrText { name: LitStr, value: IfmtInput }, - /// "attribute": true, + /// `"attribute": true` CustomAttrExpression { name: LitStr, value: Expr }, // /// onclick: move |_| {} diff --git a/packages/rsx/src/lib.rs b/packages/rsx/src/lib.rs index 7cfcc2d9f..ccfcda8fa 100644 --- a/packages/rsx/src/lib.rs +++ b/packages/rsx/src/lib.rs @@ -199,8 +199,8 @@ impl<'a> DynamicContext<'a> { let value = value.source.as_ref().unwrap(); quote! { ::dioxus::core::TemplateAttribute::Static { - name: dioxus_elements::#el_name::#name.0, - namespace: dioxus_elements::#el_name::#name.1, + name: #name, + namespace: None, value: #value, // todo: we don't diff these so we never apply the volatile flag