Toggle is hard to achieve #2291
Madoshakalaka
started this conversation in
General
Replies: 3 comments 5 replies
-
@Madoshakalaka With the power of hooks its realy easy to do that yourself: pub fn use_toggle<F>(init_fn: F) -> (bool,Callback<()>)
where
F: FnOnce() -> bool,
{
let value_handle = use_state_eq(init_fn);
let value = *value_handle;
let toggle = Callback(move |_| {
value_handle.set(!value);
});
(value,toggle)
} |
Beta Was this translation helpful? Give feedback.
3 replies
-
The #[derive(PartialEq, Default)]
struct State {
inner: bool
}
impl Reducible for State {
type Action = ();
fn reduce(self: Rc<Self>, _action: Self::Action) -> Rc<Self> {
State { inner: !self.inner }.into()
}
}
let on_or_off = use_reducer_eq(State::default); |
Beta Was this translation helpful? Give feedback.
2 replies
-
Try use_bool_toggle and use_toggle from yew_hooks: https://github.com/jetli/yew-hooks |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
correct me if I am wrong: this is the minimum code to achieve a toggling state:
a
use_toggle()
would be niceBeta Was this translation helpful? Give feedback.
All reactions