Last active
February 1, 2022 21:12
-
-
Save jochasinga/e7a662f638d4b788d38e30119d07e4a5 to your computer and use it in GitHub Desktop.
Counter app written in Yew functional components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use yew::prelude::*; | |
#[derive(Properties, PartialEq)] | |
struct TodoProps { | |
state: UseStateHandle<u64>, | |
} | |
#[function_component(Counter)] | |
fn counter(props: &TodoProps) -> Html { | |
let increment = { | |
let state = props.state.clone(); | |
Callback::from(move |_| state.set(*state + 1)) | |
}; | |
let decrement = { | |
let state = props.state.clone(); | |
Callback::from(move |_| state.set(*state - 1)) | |
}; | |
let reset = { | |
let state = props.state.clone(); | |
Callback::from(move |_| state.set(0)) | |
}; | |
html! { | |
<div class="uk-position-center uk-text-center"> | |
<button | |
onclick={increment} | |
class="uk-button uk-button-primary uk-button-large" | |
> | |
{ "+1" } | |
</button> | |
<button | |
onclick={reset} | |
class="uk-button uk-button-primary uk-button-large" | |
> | |
{ "0" } | |
</button> | |
<button | |
onclick={decrement} | |
class="uk-button uk-button-primary uk-button-large" | |
> | |
{ "-1" } | |
</button> | |
<p>{ *props.state }</p> | |
</div> | |
} | |
} | |
#[function_component(App)] | |
fn app() -> Html { | |
let state = use_state(|| 0 as u64); | |
html! { | |
<Counter {state} /> | |
} | |
} | |
fn main() { | |
yew::start_app::<App>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment