Edit in GitHubLog an issue

useWindowSize()

Constants

useWindowSize : number

The current context value for the window size context. This value updates whenever the window is resized.

Use this inside a WindowSizeContextProvider.

Functions

WindowSizeContextProvider(props)Context.Provider

This component contains a hook that listens for resize events. Use this component with useWindowSize to get the value of the resized window.

It is recommended to only create/use a single time at the top level of your app

The current context value for the window size context. This value updates whenever the window is resized.

Use this inside a WindowSizeContextProvider.

This component contains a hook that listens for resize events. Use this component with useWindowSize to get the value of the resized window.

It is recommended to only create/use a single time at the top level of your app

Summary: A React context provider.
Returns: Context.Provider — A React context provider

Parameters

NameTypeDescription
props
Object
React component props

Source Code: pwa-studio/packages/peregrine/lib/hooks/useWindowSize.js

Examples

It is recommended to only create/use the WindowSizeContextProvider a single time at the top level of your app:

Copied to your clipboard
return (
<WindowSizeContextProvider>
<App />
</WindowSizeContextProvider>
);

Inside a component in your application, use the useWindowSize() function to get the window size value that updates when the window size changes.

Copied to your clipboard
import { useWindowSize } from "@magento/peregrine";
function MyComponent(props) {
const windowSize = useWindowSize();
return (
<span>
Inner window size: {windowSize.innerWidth} x {windowSize.innerHeight}
</span>
);
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.