Security News
Supply Chain Attack Detected in @solana/web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@react-hookz/web
Advanced tools
@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.
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>;
}
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 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 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.
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
does not do so either. You'll have to
transpile your node-modules
in order to run in IE.
This package provides three levels of compilation:
/cjs
folder — CommonJS modules, with ES5 lang level./esm
folder — it is ES modules (browser compatible), with ES5 lang level./esnext
folder — it is ES modules (browser compatible), with ESNext lang level.So, if you need the useMountEffect
hook, depending on your needs, you can import in three ways
(there are actually more, but these are the three most common):
// in case you need cjs modules
import { useMountEffect } from "@react-hookz/web";
// in case you need esm modules
import { useMountEffect } from "@react-hookz/web/esm";
// in case you want all the recent ES features
import { useMountEffect } from "@react-hookz/web/esnext";
useDebouncedCallback
— Makes passed function debounced, otherwise acts like useCallback
.useRafCallback
— Makes passed function to be called within next animation frame.useThrottledCallback
— Makes passed function throttled, otherwise acts like useCallback
.useConditionalEffect
— Like useEffect
but callback invoked only if conditions match predicate.useConditionalUpdateEffect
— Like useUpdateEffect
but callback invoked only if conditions match predicate.useDebouncedEffect
— Like useEffect
, but passed function is debounced.useFirstMountState
— Return boolean that is true
only on first render.useIsMounted
— Returns function that yields current mount state.useIsomorphicLayoutEffect
— useLayoutEffect
for browser with fallback to useEffect
for SSR.useMountEffect
— Run effect only when component first-mounted.useRerender
— Return callback that re-renders component.useThrottledEffect
— Like useEffect
, but passed function is throttled.useUnmountEffect
— Run effect only when component unmounted.useUpdateEffect
— Effect hook that ignores the first render (not invoked on mount).useDebouncedState
— Lise useSafeState
but its state setter is debounced.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.useSafeState
— Like useState
, but its state setter is guarded against sets on unmounted component.useSet
— Tracks the state of a Set
.useToggle
— Like useState
, but can only become true
or false
.useThrottledState
— Like useSafeState
but its state setter is throttled.useValidator
— Performs validation when any of provided dependencies has changed.useNetworkState
— Tracks the state of browser's network connection.usePermission
— Tracks a permission state.useSyncedRef
— Like useRef
, but it returns immutable ref that contains actual value.useAsync
— Executes provided async function and tracks its result and error.useCookieValue
— Manages a single cookie.useLocalStorageValue
— Manages a single LocalStorage key.useSessionStorageValue
— Manages a single SessionStorage key.useIntersectionObserver
— Observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.useMeasure
— Uses ResizeObserver to track element dimensions and re-render component when they change.useMediaQuery
— Tracks the state of CSS media query.useResizeObserver
— Invokes a callback whenever ResizeObserver detects a change to target's size.useClickOutside
— Triggers callback when user clicks outside the target element.useDocumentTitle
— Sets title of the page.useEventListener
— Subscribes an event listener to the target, and automatically unsubscribes it on unmount.FAQs
React hooks done right, for browser and SSR.
The npm package @react-hookz/web receives a total of 116,120 weekly downloads. As such, @react-hookz/web popularity was classified as popular.
We found that @react-hookz/web demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.