百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

yew-hooks

> 前端框架
开源

Yew 的钩子,灵感来自 streamich/react-use 和 alibaba/hooks。

177 stars0 点赞5 次浏览
访问官网GitHub

工具介绍

Yew 的钩子,灵感来自 streamich/react-use 和 alibaba/hooks。

Yew Hooks

Hooks for Yew, inspired by streamich/react-use, alibaba/hooks and vueuse/vueuse.

rust
use yew_hooks::prelude::*;

#[function_component(Counter)]
fn counter() -> Html {
    let counter = use_counter(0);

    let onincrease = {
        let counter = counter.clone();
        Callback::from(move |_| counter.increase())
    };
    let ondecrease = {
        let counter = counter.clone();
        Callback::from(move |_| counter.decrease())
    };

    html! {
        <>
            { "Increase" }
            { "Decrease" }
            { "Current value: " }
            { *counter }
        
    }
}

Hooks

State

  • use_toggle - tracks state of counterparts.
  • use_bool_toggle - tracks state of a boolean.
  • use_counter - tracks state of a number.
  • use_latest - returns the latest immutable ref to state or props.
  • use_mut_latest - returns the latest mutable ref to state or props.
  • use_previous - returns the previous immutable ref to state or props.
  • use_list - tracks state of a list.
  • use_map - tracks state of a hash map.
  • use_set - tracks state of a hash set.
  • use_queue - tracks state of a queue.
  • use_raf_state - creates set method which only updates after requestAnimationFrame.
  • use_state_ptr_eq - similar to use_state_eq, but checks if the two Rcs of values point to the same allocation.
  • use_renders_count - counts component renders.
  • use_default - returns the default value when state is None.
  • use_debounce_state - debounces state.
  • use_throttle_state - throttles state.
  • use_virtual_list - provides virtual scrolling for large lists to improve performance.

Side-effects

  • use_async - resolves an async future, e.g. fetching REST api.
  • use_websocket - communicates with WebSocket.
  • use_title - sets title of the page.
  • use_favicon - sets favicon of the page.
  • use_local_storage - manages a value in localStorage.
  • use_session_storage - manages a value in sessionStorage.
  • use_cookie - manages browser cookies.
  • use_before_unload - shows browser alert when user try to reload or close the page.
  • use_debounce - debounces a function.
  • use_debounce_effect - debounces an effect.
  • use_throttle - throttles a function.
  • use_throttle_effect - throttles an effect.
  • use_clipboard - reads from or writes to clipboard for text/bytes.

Lifecycles

  • use_effect_once - a modified use_effect hook that only runs once.
  • use_effect_update - runs an effect only on updates.
  • use_mount - calls mount callbacks.
  • use_unmount - calls unmount callbacks.
  • use_is_first_mount - checks if current render is first.
  • use_is_mounted - tracks if component is mounted.
  • use_event - subscribes to events.
  • use_logger - logs in console as component goes through life cycles.

Animations

  • use_timeout - schedules a timeout to invoke callback.
  • use_interval - schedules an interval to invoke callback.
  • use_update - returns a callback, which re-renders component when called.
  • use_raf - re-renders component on each requestAnimationFrame.

Sensors

  • use_window_size - tracks Window dimensions.
  • use_window_scroll - tracks Window scroll position.
  • use_scroll - tracks an HTML element's scroll position.
  • use_scrolling - tracks whether HTML element is scrolling.
  • use_infinite_scroll - infinite scrolling of the element.
  • use_location - tracks browser's location value.
  • use_hash - tracks browser's location hash value.
  • use_search_param - tracks browser's location search param value.
  • use_size - tracks an HTML element's dimensions using the ResizeObserver API.
  • use_measure - tracks an HTML element's dimensions using the ResizeObserver API.
  • use_geolocation - tracks user's geographic location.
  • use_swipe - detects swipe based on TouchEvent.
  • use_long_press - detects when a user presses and holds an element for a specified duration.
  • use_visible - checks if an element is visible.
  • use_hovered - checks if an element is hovered.
  • use_permission - tracks browser's permission changes using the Permissions API.
  • use_idle - tracks whether the user is idle (not interacting with the page).
  • use_start_typing - triggers when user starts typing without an editable element focused.
  • use_media_query - tracks if a CSS media query matches (e.g., screen size, dark mode).

UI

  • use_click_away - triggers a callback when user clicks outside the target element.
  • use_drag - tracks file, link and copy-paste drags, used along with use_drop hook.
  • use_drop - tracks file, link and copy-paste drops.
  • use_media - plays video or audio and exposes its controls.
  • use_fullscreen - controls fullscreen mode for elements.
  • use_theme - toggles light/dark theme and persists preference (follows system preference when no explicit choice is stored).

Examples

use_counter demo

…

use_async demo

…

use_websocket demo

…

use_theme demo

A small example demonstrating the use_theme hook. The hook persists an explicit user preference to localStorage under the provided key, or follows the system prefers-color-scheme when no explicit choice exists.

rust
use yew::prelude::*;
use yew_hooks::prelude::*;

#[function_component(ThemeDemo)]
fn theme_demo() -> Html {
    // Use a storage key for persistence across reloads/tabs
    let theme = use_theme("example_theme".to_string());

    let onclick = {
        let theme = theme.clone();
        Callback::from(move |_| theme.toggle())
    };

    html! {
        <>
            { if theme.is_dark() { "Switch to light" } else { "Switch to dark" } }
            
{ format!("Active theme: {}", *theme) }

        
    }
}

Demo

Check out a live demo

Contribute

Feel free to take a look at the current issues in this repo for anything that currently needs to be worked on.

You are also welcome to open a PR or a new issue if you see something is missing or could be improved upon.

License

Apache-2.0/MIT

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rusthooksreactreact-hooksrust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架