-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the 'useWindowDimensions' hook from React Native Close #1487
- Loading branch information
Showing
5 changed files
with
77 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/react-native-web/src/exports/useWindowDimensions/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type { DisplayMetrics } from '../Dimensions'; | ||
|
||
import Dimensions from '../Dimensions'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export default function useWindowDimensions(): DisplayMetrics { | ||
const [dims, setDims] = useState(() => Dimensions.get('window')); | ||
useEffect(() => { | ||
function handleChange({ window }) { | ||
// $FlowFixMe | ||
setDims(window); | ||
} | ||
Dimensions.addEventListener('change', handleChange); | ||
// We might have missed an update between calling `get` in render and | ||
// `addEventListener` in this handler, so we set it here. If there was | ||
// no change, React will filter out this update as a no-op. | ||
setDims(Dimensions.get('window')); | ||
return () => { | ||
Dimensions.removeEventListener('change', handleChange); | ||
}; | ||
}, []); | ||
return dims; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters