What is @react-hookz/web?
@react-hookz/web is a collection of React hooks designed to simplify common web development tasks. It provides a wide range of hooks for state management, side effects, DOM interactions, and more, making it easier to build complex web applications with React.
What are @react-hookz/web's main functionalities?
State Management
The useToggle hook provides a simple way to manage boolean state. It returns the current state and a function to toggle the state.
import { useToggle } from '@react-hookz/web';
function ToggleComponent() {
const [value, toggle] = useToggle();
return (
<div>
<p>{value ? 'ON' : 'OFF'}</p>
<button onClick={toggle}>Toggle</button>
</div>
);
}
Side Effects
The useDebounce hook delays the update of a value until after a specified delay. This is useful for scenarios like search input where you want to wait for the user to stop typing before performing an action.
import { useDebounce } from '@react-hookz/web';
function SearchComponent() {
const [query, setQuery] = useState('');
const debouncedQuery = useDebounce(query, 500);
useEffect(() => {
if (debouncedQuery) {
// Perform search
}
}, [debouncedQuery]);
return (
<input
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
);
}
DOM Interactions
The useEventListener hook allows you to easily add and clean up event listeners. In this example, a click event listener is added to the window object.
import { useEventListener } from '@react-hookz/web';
function ClickLogger() {
useEventListener(window, 'click', () => {
console.log('Window clicked');
});
return <div>Click anywhere to log a message</div>;
}
Other packages similar to @react-hookz/web
react-use
react-use is a collection of essential React hooks. It offers a wide range of hooks for state management, side effects, lifecycle events, and more. Compared to @react-hookz/web, react-use has a larger community and more extensive documentation.
ahooks
ahooks is a React hooks library that provides a set of high-quality and reliable hooks. It focuses on performance and ease of use. ahooks offers similar functionalities to @react-hookz/web but also includes some unique hooks for advanced use cases.
usehooks-ts
usehooks-ts is a collection of React hooks written in TypeScript. It provides type-safe hooks for common tasks in React applications. Compared to @react-hookz/web, usehooks-ts is more focused on TypeScript users and offers type safety out of the box.
@react-hookz/web
is a library of general-purpose React hooks built with care and SSR compatibility
in mind.
Install
This one is pretty simple, everyone knows what to do:
npm i @react-hookz/web
# or
yarn add @react-hookz/web
As hooks was introduced to the world in React 16.8, @react-hookz/web
requires - you guessed it -
react
and react-dom
16.8+.
Also, as React does not support IE, @react-hookz/web
don't either.
Usage
This package distributed with ESNext language level and both, CJS and ES imports.
It means that depending on your browser target you might need to transpile it. Every major
bundler provides a way to transpile node_modules
fully or partially.
Address your bundler documentation for more details.
You can import hooks two ways:
import { useMountEffect } from '@react-hookz/web';
import { useMountEffect } from '@react-hookz/web/esm/useMountEffect';
In case your bundler supports tree-shaking (most of modern does) - both variants are equal and only
necessary code will get into your bundle. Direct hook imports should be considered otherwise.
In case, for some reason, you are not able to use ES imports - you should direct-import hooks from
@react-hookz/web/esm
folder.
Migrating from react-use
@react-hookz/web
was built as a spiritual successor
of react-use
by one of its former maintainers.
Coming from react-use
? Check out our
migration guide.
Hooks list
-
Callback
-
Lifecycle
-
State
useControlledRerenderState
— Like useState
, but its state setter accepts an extra argument, that allows cancelling
renders.useCounter
— Tracks a numeric value and offers functions for manipulating it.useDebouncedState
— Like useState
but its state setter is debounced.useFunctionalState
— Like useState
but instead of raw state, a state getter function is returned.useList
— Tracks a list and offers functions for manipulating it.useMap
— Tracks the
state of a Map
.useMediatedState
— Like useState
, but every value set is passed through a mediator function.usePrevious
—
Returns the value passed to the hook on previous render.usePreviousDistinct
—
Returns the most recent distinct value passed to the hook on previous renders.useQueue
—
A state hook implementing FIFO queue.useRafState
—
Like React.useState
, but state is only updated within animation frame.useRenderCount
—
Tracks component's render count including first render.useSet
— Tracks the
state of a Set
.useToggle
— Like
useState
, but can only be true
or false
.useThrottledState
— Like useState
but its state setter is throttled.useValidator
— Performs validation when any of the provided dependencies change.
-
Navigator
-
Miscellaneous
useSyncedRef
— Like useRef
, but it returns an immutable ref that contains the actual value.useCustomCompareMemo
— Like useMemo
but uses provided comparator function to validate dependency changes.useDeepCompareMemo
— Like useMemo
but uses @react-hookz/deep-equal
comparator function to validate deep
dependency changes.useHookableRef
— Like useRef
but it is possible to define handlers for getting and setting the value.
-
Side-effect
-
Sensor
useIntersectionObserver
— Observe changes in the intersection of a target element with an ancestor element or with the
viewport.useMeasure
—
Uses ResizeObserver
to track an element's dimensions and to re-render the component when they change.useMediaQuery
— Tracks the state of a CSS media query.useResizeObserver
— Invokes a callback whenever ResizeObserver
detects a change to the target's size.useScreenOrientation
— Checks if the screen is in portrait
or landscape
orientation and automatically re-renders on
orientation change.useDocumentVisibility
— Tracks document visibility state.
-
Dom
useClickOutside
— Triggers a callback when the user clicks outside a target element.useEventListener
— Subscribes an event listener to a target element.useKeyboardEvent
— Invokes a callback when a keyboard event occurs on the chosen target.useWindowSize
— Tracks the inner dimensions of the browser window.
Contributors