Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active February 1, 2022 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jochasinga/e7a662f638d4b788d38e30119d07e4a5 to your computer and use it in GitHub Desktop.
Save jochasinga/e7a662f638d4b788d38e30119d07e4a5 to your computer and use it in GitHub Desktop.
Counter app written in Yew functional components
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