Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@react-hook/window-size
Advanced tools
React hooks for updating components when the size of the `window` changes.
@react-hook/window-size is a React hook that allows you to easily get the current window size and respond to window resize events. It provides a simple API to access the width and height of the window, making it useful for responsive design and dynamic layout adjustments.
Get Window Size
This feature allows you to get the current width and height of the window. The `useWindowSize` hook returns an array with the width and height, which you can use to render responsive components.
import { useWindowSize } from '@react-hook/window-size';
function MyComponent() {
const [width, height] = useWindowSize();
return (
<div>
<p>Window width: {width}px</p>
<p>Window height: {height}px</p>
</div>
);
}
Custom Throttle
This feature allows you to throttle the resize event updates. By passing a number to `useWindowSize`, you can control how often the window size is updated, which can improve performance by reducing the frequency of re-renders.
import { useWindowSize } from '@react-hook/window-size';
function MyComponent() {
const [width, height] = useWindowSize(250); // Throttle updates every 250ms
return (
<div>
<p>Window width: {width}px</p>
<p>Window height: {height}px</p>
</div>
);
}
react-use is a collection of essential React hooks, including a `useWindowSize` hook. It provides similar functionality to @react-hook/window-size but also includes a wide variety of other hooks for different use cases, making it a more comprehensive solution for managing state and side effects in React applications.
react-responsive is a package that allows you to create media query components in React. While it doesn't provide a direct hook for window size, it offers a more declarative approach to handling responsive design by allowing you to define breakpoints and render different components based on the current viewport size.
use-resize-observer is a React hook that uses the ResizeObserver API to track the size of a DOM element. While it is more focused on element size rather than window size, it can be used to achieve similar results by observing the document's root element. It provides more granular control over resizing behavior compared to @react-hook/window-size.
React hooks for updating components when the size of the window
changes.
npm i @react-hook/window-size
yarn add @react-hook/window-size
// using debounced values
import {
useWindowSize,
useWindowWidth,
useWindowHeight
} from '@react-hook/window-size'
const F = props => {
const [width, height] = useWindowSize(
360 /* initialWidth when there is no window */,
720 /* initialHeight when there is no window */,
{wait: 100}
)
}
// using throttled values
import {
useWindowSize,
useWindowWidth,
useWindowHeight
} from '@react-hook/window-size/throttled'
const F = props => {
const [width, height] = useWindowSize(
360 /* initialWidth when there is no window */,
720 /* initialHeight when there is no window */,
{fps: 30}
)
}
useWindowSize(initialWidth?: number, initialHeight?: number, debounceOptions?: object)
initialWidth
<number>
window
objectinitialHeight
<number>
window
objectdebounceOptions
<object>
wait
<number>
setState
to wait after the
last received action before executingleading
<bool>
setState
on the leading edge (right away). When false
setState
will not be called until the next frame is due[width: integer, height: integer]: array
width
height
useWindowWidth(initialWidth?: number, debounceOptions?: object)
initialWidth
<number>
window
objectdebounceOptions
<object>
wait
<number>
setState
to wait after the
last received action before executingleading
<bool>
setState
on the leading edge (right away). When false
setState
will not be called until the next frame is duewidth: integer
width
useWindowHeight(initialHeight?: number, debounceOptions?: object)
initialHeight
<number>
window
objectdebounceOptions
<object>
wait
<number>
setState
to wait after the
last received action before executingleading
<bool>
setState
on the leading edge (right away). When false
setState
will not be called until the next frame is dueheight: integer
height
import {
useWindowSize,
useWindowWidth,
useWindowHeight,
} from '@react-hook/toggle/throttled'
useWindowSize(initialWidth?: number, initialHeight?: number, throttleOptions?: object)
initialWidth
<number>
window
objectinitialHeight
<number>
window
objectthrottleOptions
<object>
fps
<number>
leading
<bool>
setState
on the leading edge (right away). When false
setState
will not be called until the next frame is due[width: integer, height: integer]: array
width
height
useWindowWidth(initialWidth?: number, throttleOptions?: object)
initialWidth
<number>
window
objectthrottleOptions
<object>
fps
<number>
leading
<bool>
setState
on the leading edge (right away). When false
setState
will not be called until the next frame is duewidth: integer
width
useWindowHeight(initialHeight?: number, throttleOptions?: object)
initialHeight
<number>
window
objectthrottleOptions
<object>
fps
<number>
leading
<bool>
setState
on the leading edge (right away). When false
setState
will not be called until the next frame is dueheight: integer
height
FAQs
React hooks for updating components when the size of the `window` changes.
The npm package @react-hook/window-size receives a total of 106,388 weekly downloads. As such, @react-hook/window-size popularity was classified as popular.
We found that @react-hook/window-size demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.