1 release (0 unstable)

1.0.0-alpha.1 Nov 17, 2022

#158 in #yew


Used in hooks-yew

MIT license

12KB
256 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

~1.5MB
~41K SLoC