Skip to content

Commit

Permalink
Simplify component state
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed May 13, 2020
1 parent a90885c commit 40a783f
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 236 deletions.
2 changes: 1 addition & 1 deletion yew-functional/src/lib.rs
Expand Up @@ -260,7 +260,7 @@ where
}
use_hook(
move |state: &mut UseEffectState<Dependents, Destructor>, hook_update| {
let mut should_update = !(*state.deps == *deps);
let mut should_update = *state.deps != *deps;

move || {
hook_update(move |state: &mut UseEffectState<Dependents, Destructor>| {
Expand Down
37 changes: 20 additions & 17 deletions yew-functional/tests/lib.rs
Expand Up @@ -151,48 +151,51 @@ fn use_effect_destroys_on_component_drop() {
struct UseEffectFunction {}
struct UseEffectWrapper {}
#[derive(Properties, Clone)]
struct DestroyCalledProps {
struct WrapperProps {
destroy_called: Rc<dyn Fn()>,
}
impl PartialEq for DestroyCalledProps {
impl PartialEq for WrapperProps {
fn eq(&self, _other: &Self) -> bool {
false
}
}
#[derive(Properties, Clone)]
struct FunctionProps {
effect_called: Rc<dyn Fn()>,
destroy_called: Rc<dyn Fn()>,
}
impl PartialEq for FunctionProps {
fn eq(&self, _other: &Self) -> bool {
false
}
}
type UseEffectComponent = FunctionComponent<UseEffectFunction>;
type UseEffectWrapperComponent = FunctionComponent<UseEffectWrapper>;
impl FunctionProvider for UseEffectFunction {
type TProps = DestroyCalledProps;
type TProps = FunctionProps;

fn run(props: &Self::TProps) -> Html {
let effect_called = props.effect_called.clone();
let destroy_called = props.destroy_called.clone();
use_effect_with_deps(
move |_| {
move || {
destroy_called();
}
effect_called();
move || destroy_called()
},
(),
);
return html! {};
}
}
impl FunctionProvider for UseEffectWrapper {
type TProps = DestroyCalledProps;
type TProps = WrapperProps;

fn run(props: &Self::TProps) -> Html {
let (show, set_show) = use_state(|| true);
use_effect_with_deps(
move |_| {
set_show(false);
|| {}
},
(),
);

if *show {
let effect_called: Rc<dyn Fn()> = Rc::new(move || set_show(false));
return html! {
<UseEffectComponent destroy_called=props.destroy_called.clone() />
<UseEffectComponent destroy_called=props.destroy_called.clone() effect_called=effect_called />
};
} else {
return html! {
Expand All @@ -206,7 +209,7 @@ fn use_effect_destroys_on_component_drop() {
let destroy_counter_c = destroy_counter.clone();
app.mount_with_props(
yew::utils::document().get_element_by_id("output").unwrap(),
DestroyCalledProps {
WrapperProps {
destroy_called: Rc::new(move || *destroy_counter_c.borrow_mut().deref_mut() += 1),
},
);
Expand Down

0 comments on commit 40a783f

Please sign in to comment.