#yew #hooks

hooks-yew

Compile-time hook component for yew

2 releases

1.0.0-alpha.2 Nov 19, 2022
1.0.0-alpha.1 Nov 17, 2022

MIT license

6KB
98 lines

hooks-yew

Compile-time hooks component for yew, implemented with hooks.

This crate is still in alpha.

Add hooks and hooks-yew to your project.

cargo add hooks hooks-yew
use yew::prelude::*;

use hooks::use_state;
use hooks_yew::hook_component;

#[hook_component]
fn Counter() {
    let (state, updater) = use_state::<i32>(0);
    let updater = updater.clone();

    html! {
        <div>
            <button onclick={move |_| updater.replace_with_fn_pointer(|v| *v + 1)}>{ "+1" }</button>
            {state}
        </div>
    }
}

fn main() {
    yew::start_app::<Counter>();
}

Specify props with an argument:

#[hook_component]
fn Counter(props: Props) {
    // ...
}

Specify return type explicitly. (It must be yew::Html)

#[hook_component]
fn Counter(props: Props) -> ::yew::Html {
    // ...
}

Dependencies

~13MB
~248K SLoC