Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix useWindowDimensions hook #26008

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Libraries/Utilities/useWindowDimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ import {type DisplayMetrics} from './NativeDeviceInfo';
import * as React from 'react';

export default function useWindowDimensions(): DisplayMetrics {
const dims = Dimensions.get('window'); // always read the latest value
const forceUpdate = React.useState(false)[1].bind(null, v => !v);
const initialDims = React.useState(dims)[0];
const [dims, setDims] = React.useState(Dimensions.get('window')); // set initial value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Delete ·

React.useEffect(() => {
Dimensions.addEventListener('change', forceUpdate);

const latestDims = Dimensions.get('window');
if (latestDims !== initialDims) {
// We missed an update between calling `get` in render and
// `addEventListener` in this handler...
forceUpdate();
function handleChange({window}) {
setDims(window)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Insert ;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semi: Missing semicolon.

}
Dimensions.addEventListener('change', handleChange);
return () => {
Dimensions.removeEventListener('change', forceUpdate);
Dimensions.removeEventListener('change', handleChange);
};
}, [forceUpdate, initialDims]);
}, []);
return dims;
}