>().expect("context not passed down");
-
- Ok(html! {
- <>
- { format!("magic: {}\n", props.magic) }
-
- { format!("current: {}, total: {}", ctx.0, counter.borrow()) }
-
- >
- })
+
+ #[function_component]
+ fn ContextOutlet(props: &ContextOutletProps) -> Html {
+ let counter = use_mut_ref(|| 0);
+ *counter.borrow_mut() += 1;
+
+ let ctx = use_context::>().expect("context not passed down");
+
+ html! {
+ <>
+ { format!("magic: {}\n", props.magic) }
+
+ { format!("current: {}, total: {}", ctx.0, counter.borrow()) }
+
+ >
}
}
- type ContextOutlet = FunctionComponent;
-
- struct TestFunction;
- impl FunctionProvider for TestFunction {
- type TProps = ();
-
- fn run(_props: &Self::TProps) -> HtmlResult {
- type MyContextProvider = ContextProvider>;
-
- let ctx = use_state(|| MyContext("hello".into()));
- let rendered = use_mut_ref(|| 0);
-
- // this is used to force an update specific to test-2
- let magic_rc = use_state(|| 0);
- let magic: usize = *magic_rc;
- {
- let ctx = ctx.clone();
- use_effect(move || {
- let count = *rendered.borrow();
- match count {
- 0 => {
- ctx.set(MyContext("world".into()));
- *rendered.borrow_mut() += 1;
- }
- 1 => {
- // force test-2 to re-render.
- magic_rc.set(1);
- *rendered.borrow_mut() += 1;
- }
- 2 => {
- ctx.set(MyContext("hello world!".into()));
- *rendered.borrow_mut() += 1;
- }
- _ => (),
- };
- || {}
- });
- }
- Ok(html! {
-
-
-
-
-
-
- })
+
+ #[function_component]
+ fn TestComponent() -> Html {
+ type MyContextProvider = ContextProvider>;
+
+ let ctx = use_state(|| MyContext("hello".into()));
+ let rendered = use_mut_ref(|| 0);
+
+ // this is used to force an update specific to test-2
+ let magic_rc = use_state(|| 0);
+ let magic: usize = *magic_rc;
+ {
+ let ctx = ctx.clone();
+ use_effect(move || {
+ let count = *rendered.borrow();
+ match count {
+ 0 => {
+ ctx.set(MyContext("world".into()));
+ *rendered.borrow_mut() += 1;
+ }
+ 1 => {
+ // force test-2 to re-render.
+ magic_rc.set(1);
+ *rendered.borrow_mut() += 1;
+ }
+ 2 => {
+ ctx.set(MyContext("hello world!".into()));
+ *rendered.borrow_mut() += 1;
+ }
+ _ => (),
+ };
+ || {}
+ });
+ }
+ html! {
+
+
+
+
+
+
}
}
- type TestComponent = FunctionComponent;
yew::start_app_in_element::(
gloo_utils::document().get_element_by_id("output").unwrap(),
diff --git a/packages/yew/tests/use_effect.rs b/packages/yew/tests/use_effect.rs
index bf064a87064..594d607dcb7 100644
--- a/packages/yew/tests/use_effect.rs
+++ b/packages/yew/tests/use_effect.rs
@@ -4,17 +4,12 @@ use common::obtain_result;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use wasm_bindgen_test::*;
-use yew::functional::{
- use_effect_with_deps, use_mut_ref, use_state, FunctionComponent, FunctionProvider,
-};
-use yew::{html, HtmlResult, Properties};
+use yew::prelude::*;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn use_effect_destroys_on_component_drop() {
- struct UseEffectFunction {}
- struct UseEffectWrapper {}
#[derive(Properties, Clone)]
struct WrapperProps {
destroy_called: Rc,
@@ -34,42 +29,37 @@ fn use_effect_destroys_on_component_drop() {
false
}
}
- type UseEffectComponent = FunctionComponent;
- type UseEffectWrapperComponent = FunctionComponent;
- impl FunctionProvider for UseEffectFunction {
- type TProps = FunctionProps;
-
- fn run(props: &Self::TProps) -> HtmlResult {
- let effect_called = props.effect_called.clone();
- let destroy_called = props.destroy_called.clone();
- use_effect_with_deps(
- move |_| {
- effect_called();
- #[allow(clippy::redundant_closure)] // Otherwise there is a build error
- move || destroy_called()
- },
- (),
- );
- Ok(html! {})
- }
+
+ #[function_component(UseEffectComponent)]
+ fn use_effect_comp(props: &FunctionProps) -> Html {
+ let effect_called = props.effect_called.clone();
+ let destroy_called = props.destroy_called.clone();
+ use_effect_with_deps(
+ move |_| {
+ effect_called();
+ #[allow(clippy::redundant_closure)] // Otherwise there is a build error
+ move || destroy_called()
+ },
+ (),
+ );
+ html! {}
}
- impl FunctionProvider for UseEffectWrapper {
- type TProps = WrapperProps;
-
- fn run(props: &Self::TProps) -> HtmlResult {
- let show = use_state(|| true);
- if *show {
- let effect_called: Rc = { Rc::new(move || show.set(false)) };
- Ok(html! {
-
- })
- } else {
- Ok(html! {
- { "EMPTY" }
- })
+
+ #[function_component(UseEffectWrapperComponent)]
+ fn use_effect_wrapper_comp(props: &WrapperProps) -> Html {
+ let show = use_state(|| true);
+ if *show {
+ let effect_called: Rc = { Rc::new(move || show.set(false)) };
+ html! {
+