Expand description
§Yew Hooks
Hooks for Yew, inspired by streamich/react-use and alibaba/hooks.
§Examples
use yew::prelude::*;
use yew_hooks::use_counter;
#[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())
};
let onincreaseby = {
let counter = counter.clone();
Callback::from(move |_| counter.increase_by(10))
};
let ondecreaseby = {
let counter = counter.clone();
Callback::from(move |_| counter.decrease_by(10))
};
let onset = {
let counter = counter.clone();
Callback::from(move |_| counter.set(100))
};
let onreset = {
let counter = counter.clone();
Callback::from(move |_| counter.reset())
};
html! {
<div>
<button onclick={onincrease}>{ "Increase" }</button>
<button onclick={ondecrease}>{ "Decrease" }</button>
<button onclick={onincreaseby}>{ "Increase by 10" }</button>
<button onclick={ondecreaseby}>{ "Decrease by 10" }</button>
<button onclick={onset}>{ "Set to 100" }</button>
<button onclick={onreset}>{ "Reset" }</button>
<p>
<b>{ "Current value: " }</b>
{ *counter }
</p>
</div>
}
}§Demo
Modules§
Structs§
- Close
Event - The
CloseEventclass. - Cookie
Attributes - Attributes for setting cookies
- Location
State - State for brower’s location.
- UseAsync
Handle - State handle for the
use_asynchook. - UseAsync
Options - Options for
use_async_with_options. - UseAsync
State - State for an async future.
- UseClipboard
Handle - State handle for the
use_clipboardhook. - UseCookie
Handle - State handle for the
use_cookiehook. - UseCounter
Handle - State handle for the
use_counterhook. - UseDebounce
Handle - State handle for the
use_debouncehook. - UseDebounce
State Handle - State handle for the
use_debounce_statehook. - UseDefault
Handle - State handle for the
use_defaulthook. - UseDrag
Handle - State handle for the
use_draghook. - UseDrag
Options - Options for drag.
- UseDrop
Handle - State handle for the
use_drophook. - UseDrop
Options - Options for drop.
- UseFullscreen
Handle - State handle for the
use_fullscreenhook. - UseGeolocation
Options - The
PositionOptionsdictionary. - UseGeolocation
State - UseHash
Handle - State handle for the
use_hashhook. - UseIdle
Handle - State handle for the
use_idlehook. - UseIdle
Options - Configuration options for the
use_idlehook. - UseLatest
Handle - State handle for the
use_latesthook. - UseList
Handle - State handle for the
use_listhook. - UseLocal
Storage Handle - State handle for the
use_local_storagehook. - UseLong
Press Handle - State handle for the
use_long_presshook. - UseLong
Press Options - Options for long press.
- UseMap
Handle - State handle for the
use_maphook. - UseMeasure
State - UseMedia
Handle - State handle for the
use_mediahook. - UseMedia
Options - Options for media
- UseMut
Latest Handle - State handle for the
use_mut_latesthook. - UsePrevious
Handle - State handle for the
use_previoushook. - UseQueue
Handle - State handle for the
use_queuehook. - UseRaf
State Handle - State handle for the
use_raf_statehook. - UseSession
Storage Handle - State handle for the
use_session_storagehook. - UseSet
Handle - State handle for the
use_sethook. - UseState
PtrEq Handle - State handle for the
use_state_ptr_eqhook. - UseSwipe
Handle - State handle for the
use_swipehook. - UseSwipe
Options - Options for swipe.
- UseTheme
Handle - State handle for the
use_themehook. - UseThrottle
Handle - State handle for the
use_throttlehook. - UseThrottle
State Handle - State handle for the
use_throttle_statehook. - UseTimeout
Handle - State handle for the
use_timeouthook. - UseToggle
Handle - State handle for the
use_togglehook. - UseVirtual
List Handle - State handle for the
use_virtual_listhook. - UseWeb
Socket Handle - State handle for the
use_websockethook. - UseWeb
Socket Options - Options for
WebSocket. - Virtual
List Item - State handle for the
use_virtual_listhook.
Enums§
- Same
Site - SameSite attribute values
- UseSwipe
Direction - Swipe direction.
- UseWeb
Socket Ready State - The current state of the
WebSocketconnection.
Functions§
- use_
async - This hook returns state and a
runcallback for an async future. - use_
async_ with_ options - This hook returns state and a
runcallback for an async future with options. Seeuse_asynctoo. - use_
before_ unload - A side-effect hook that shows browser alert when user try to reload or close the page.
- use_
bool_ toggle - This hook is a simplified
use_toggleto manage boolean toggle state in a function component. - use_
click_ away - A hook that triggers a callback when user clicks outside the target element.
- use_
clipboard - This hook is used to read from or write to clipboard for text or bytes.
e.g. copy plain text or copy
image/pngfile to clipboard. - use_
cookie - A side-effect hook that manages a single cookie.
- use_
counter - This hook is used to manage counter state in a function component.
- use_
debounce - A hook that delays invoking a function until after wait milliseconds have elapsed since the last time the debounced function was invoked.
- use_
debounce_ effect - A hook that delays calling effect callback until after wait milliseconds have elapsed since the last time effect callback was called.
- use_
debounce_ effect_ with_ deps - This hook is similar to
use_debounce_effectbut it accepts dependencies. - use_
debounce_ state - A hook that delays updating state until after wait milliseconds have elapsed since the last time state was updated.
- use_
default - A state hook that returns the default value when state is None.
- use_
drag - This hook tracks file, link and copy-paste drags.
- use_
drag_ with_ options - This hook tracks file, link and copy-paste drags.
use_draghook with options. - use_
drop - This hook tracks file, link and copy-paste drops.
- use_
drop_ with_ options - This hook tracks file, link and copy-paste drops.
use_drophook with options. - use_
effect_ once - A lifecycle hook that runs an effect only once.
- use_
effect_ update - This hook ignores the first invocation (e.g. on mount).
The signature is exactly the same as the
use_effecthook. - use_
effect_ update_ with_ deps - This hook is similar to
use_effect_updatebut it accepts dependencies. The signature is exactly the same as theuse_effect_withhook. - use_
event - A hook that subscribes a callback to events.
- use_
event_ with_ window - A hook that subscribes a callback to events only for window.
If you want to specify an event target, use
use_event. - use_
favicon - A side-effect hook that sets favicon of the page.
- use_
fullscreen - This hook provides functionality to control fullscreen mode for elements. It allows entering, exiting, and toggling fullscreen mode, and tracks the current fullscreen state and element.
- use_
geolocation - A sensor hook that tracks user’s geographic location.
- use_
geolocation_ with_ options - A sensor hook that tracks user’s geographic location.
See
use_geolocation - use_
hash - A sensor hook that tracks brower’s location hash value.
- use_
hovered - A sensor hook that tracks whether HTML element is being hovered.
- use_
idle - This hook tracks whether the user is idle (not interacting with the page). It listens for various user events (mouse, keyboard, scroll, etc.) and resets an internal timer. When the timer expires, the user is considered idle.
- use_
idle_ with_ options - This hook tracks whether the user is idle with custom configuration options.
- use_
infinite_ scroll - A sensor hook that tracks infinite scrolling of the element.
- use_
interval - A hook that schedules an interval to invoke
callbackeverymillismilliseconds. The interval will be cancelled ifmillisis set to 0. - use_
is_ first_ mount - A hook returns true if component is just mounted (on first render) and false otherwise.
- use_
is_ mounted - A hook returns true if component is mounted and false otherwise.
- use_
latest - This hook returns the latest immutable ref to state or props.
- use_
list - A hook that tracks a list and provides methods to modify it.
- use_
local_ storage - A side-effect hook that manages a single localStorage key.
- use_
location - A sensor hook that tracks brower’s location value.
- use_
logger - This hook logs in console as component goes through life-cycles.
- use_
logger_ eq - This hook logs in console as component goes through life-cycles.
Like
use_loggerbut only logs whenprev_state != next_state. This requires the props to implementPartialEq. - use_
long_ press - A hook that detects when a user presses and holds an element for a specified duration.
- use_
long_ press_ with_ options - A hook that detects when a user presses and holds an element for a specified duration with options.
- use_map
- A hook that tracks a hash map and provides methods to modify it.
- use_
measure - A sensor hook that tracks an HTML element’s dimensions using the
ResizeObserverAPI. - use_
media - This hook plays video or audio and exposes its controls.
- use_
media_ with_ options - This hook plays video or audio and exposes its controls with options.
see
use_media - use_
mount - A lifecycle hook that calls a function after the component is mounted.
- use_
mut_ latest - This hook returns the latest mutable ref to state or props.
- use_
permission - A sensor hook that tracks browser’s permission changes
- use_
previous - This hook returns the previous immutable ref to state or props.
- use_
queue - A hook that tracks a queue and provides methods to modify it.
- use_raf
- An animation hook that forces component to re-render on each
requestAnimationFrame, returns percentage of time elapsed.millis- milliseconds for how long to keep re-rendering component.delay— delay in milliseconds after which to start re-rendering component. - use_
raf_ state - A state hook that only updates state in the callback of
requestAnimationFrame. - use_
renders_ count - A hook that counts component renders.
- use_
scroll - A sensor hook that tracks an HTML element’s scroll position.
- use_
scrolling - A sensor hook that tracks whether HTML element is scrolling.
- use_
search_ param - A sensor hook that tracks brower’s location search param value.
- use_
session_ storage - A side-effect hook that manages a single sessionStorage key.
- use_set
- A hook that tracks a hash set and provides methods to modify it.
- use_
size - A sensor hook that tracks an HTML element’s dimensions using the
ResizeObserverAPI. - use_
state_ ptr_ eq - Similar to
use_state_eq, but check if the twoRcs of values point to the same allocation, instead of PartialEq of the values. - use_
swipe - A sensor hook that detects swipe based on TouchEvent.
- use_
swipe_ with_ options - A sensor hook that detects swipe based on TouchEvent with options.
If you want to detect for window, pass
NodeRef::default()to paramnode. - use_
swipe_ with_ window - A sensor hook that detects swipe based on TouchEvent for window.
See
use_swipe. - use_
theme - A hook to manage light/dark theme with persistence and system preference support.
- use_
throttle - A hook that throttles invoking a function, the function is only executed once every
millis. - use_
throttle_ effect - A hook that throttles calling effect callback, it is only called once every
millis. - use_
throttle_ effect_ with_ deps - This hook is similar to
use_throttle_effectbut it accepts dependencies. - use_
throttle_ state - A hook that throttles updating state, the state is only updated once every
millis. - use_
timeout - A hook that schedules a timeout to invoke
callbackinmillismilliseconds from now. The timeout will be cancelled ifmillisis set to 0 orcancel()is called. - use_
title - A side-effect hook that sets title of the page and restore previous title when unmount.
- use_
toggle - This hook is used to manage toggle state in a function component.
- use_
unmount - A lifecycle hook that calls a function when the component will unmount.
- use_
update - A hook returns a function that forces component to re-render when called.
- use_
virtual_ list - A hook that provides virtual scrolling for large lists.
- use_
visible - Check if an element is visible. Internally, it uses an
IntersectionObserverto receive notifications from the browser whenever the visibility state of the node changes. - use_
websocket - This hook communicates with
WebSocket. - use_
websocket_ with_ options - This hook communicates with
WebSocketwith options. - use_
window_ scroll - A sensor hook that tracks Window scroll position.
- use_
window_ size - A sensor hook that tracks dimensions of the browser window.