pub fn use_context<T: Clone + PartialEq + 'static>() -> Option<Rc<T>> {
    struct UseContextState<T2: Clone + PartialEq + 'static> {
        provider_scope: Option<Scope<ContextProvider<T2>>>,
        current_context: Option<Rc<T2>>,
        callback: Option<Rc<ConsumerCallback<T2>>>,
    }
    impl<T: Clone + PartialEq + 'static> Hook for UseContextState<T> {
        fn tear_down(&mut self) {
            if let Some(cb) = self.callback.take() {
                drop(cb);
            }
        }
    }

    let scope = get_current_scope()
        .expect("No current Scope. `use_context` can only be called inside function components");

    use_hook(
        |state: &mut UseContextState<T>, hook_callback| {
            state.callback = Some(Rc::new(Box::new(move |ctx: Rc<T>| {
                hook_callback(
                    |state: &mut UseContextState<T>| {
                        state.current_context = Some(ctx);
                        true
                    },
                    false, // run pre render
                );
            })));
            let weak_cb = Rc::downgrade(state.callback.as_ref().unwrap());
            with_provider_component(&state.provider_scope, |comp| {
                comp.subscribe_consumer(weak_cb)
            });

            state.current_context.clone()
        },
        move || {
            let provider_scope = find_context_provider_scope::<T>(&scope);
            let current_context =
                with_provider_component(&provider_scope, |comp| Rc::clone(&comp.context));
            UseContextState {
                provider_scope,
                current_context,
                callback: None,
            }
        },
    )
}
/// Hook for consuming context values in function components.
/// The context of the type passed as `T` is returned. If there is no such context in scope, `None` 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).
///
/// # Example
/// ```rust
/// # use yew_functional::{function_component, use_context};
/// # use yew::prelude::*;
/// # use std::rc::Rc;
///
/// # #[derive(Clone, Debug, PartialEq)]
/// # struct ThemeContext {
/// #    foreground: String,
/// #    background: String,
/// # }
/// #[function_component(ThemedButton)]
/// pub fn themed_button() -> Html {
///     let theme = use_context::<Rc<ThemeContext>>().expect("no ctx found");
///
///     html! {
///         <button style=format!("background: {}; color: {}", theme.background, theme.foreground)>
///             { "Click me" }
///         </button>
///     }
/// }
/// ```
Hook for consuming context values in function components.
The context of the type passed as `T` is returned. If there is no such context in scope, `None` 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].
Hook for consuming context values in function components . The context of the type passed as ` T ` is returned . If there is no such context in scope ` None ` 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 ] .
pub fn use_context<T: Clone + PartialEq + 'static>() -> Option<Rc<T>> {
    struct UseContextState<T2: Clone + PartialEq + 'static> {
        provider_scope: Option<Scope<ContextProvider<T2>>>,
        current_context: Option<Rc<T2>>,
        callback: Option<Rc<ConsumerCallback<T2>>>,
    }
    impl<T: Clone + PartialEq + 'static> Hook for UseContextState<T> {
        fn tear_down(&mut self) {
            if let Some(cb) = self.callback.take() {
                drop(cb);
            }
        }
    }
    let scope = get_current_scope()
        .expect("No current Scope. `use_context` can only be called inside function components");
    use_hook(
        |state: &mut UseContextState<T>, hook_callback| {
            state.callback = Some(Rc::new(Box::new(move |ctx: Rc<T>| {
                hook_callback(
                    |state: &mut UseContextState<T>| {
                        state.current_context = Some(ctx);
                        true
                    },
                    false, // run pre render
                );
            })));
            let weak_cb = Rc::downgrade(state.callback.as_ref().unwrap());
            with_provider_component(&state.provider_scope, |comp| {
                comp.subscribe_consumer(weak_cb)
            });
            state.current_context.clone()
        },
        move || {
            let provider_scope = find_context_provider_scope::<T>(&scope);
            let current_context =
                with_provider_component(&provider_scope, |comp| Rc::clone(&comp.context));
            UseContextState {
                provider_scope,
                current_context,
                callback: None,
            }
        },
    )
}
pub fn use_context < T : Clone + PartialEq + ' static > ( ) -> Option < Rc < T > > { struct UseContextState < T2 : Clone + PartialEq + ' static > { provider_scope : Option < Scope < ContextProvider < T2 > > > , current_context : Option < Rc < T2 > > , callback : Option < Rc < ConsumerCallback < T2 > > > , } impl < T : Clone + PartialEq + ' static > Hook for UseContextState < T > { fn tear_down ( & mut self ) { if let Some ( cb ) = self . callback . take ( ) { drop ( cb ) ; } } } let scope = get_current_scope ( ) . expect ( " " ) ; use_hook ( | state : & mut UseContextState < T > , hook_callback | { state . callback = Some ( Rc :: new ( Box :: new ( move | ctx : Rc < T > | { hook_callback ( | state : & mut UseContextState < T > | { state . current_context = Some ( ctx ) ; true } , false , // run pre render ) ; } ) ) ) ; let weak_cb = Rc :: downgrade ( state . callback . as_ref ( ) . unwrap ( ) ) ; with_provider_component ( & state . provider_scope , | comp | { comp . subscribe_consumer ( weak_cb ) } ) ; state . current_context . clone ( ) } , move | | { let provider_scope = find_context_provider_scope :: < T > ( & scope ) ; let current_context = with_provider_component ( & provider_scope , | comp | Rc :: clone ( & comp . context ) ) ; UseContextState { provider_scope , current_context , callback : None , } } , ) }
Hook for consuming context values in function components.
Hook for consuming context values in function components .
example
None
use yew_functional::{function_component, use_context};
None
use yew::prelude::*;
None
use std::rc::rc;
None
#[derive(clone, debug, partialeq)]
None
struct themecontext {
None
foreground: string,
None
background: string,
None
}
None
[function_component(themedbutton)]
pub fn themed_button() -> Html {
let theme = use_context::>().expect("no ctx found").

html.
pub fn themed_button () - > Html { let theme = use_context :: > () . expect ( " no ctx found " ) . html .