-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
useWindowDimensions causes infinite re-rendering #26733
Comments
But are you trying to setState on useEffect? |
Not that I know of. I'm isolating pieces of code, trying to triage the issue. |
You can see in the source code that there is a setState on useEffect. |
Can confirm, I came here because of this problem. I thought it could be storybook configuration blasting my CPU to 100%, but after changing
|
How can one even use the hook? |
@ShaMan123 turns out it works, just TypeScript is complaining that |
yeah. They forgot to update |
+1 Had to replace by:
Happened after leaving the app and coming back |
Interestingly, this is the code actually shipped with react-native (0.61.4), and it causes the infinite loop. /**
* 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 Dimensions from './Dimensions';
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];
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();
}
return () => {
Dimensions.removeEventListener('change', forceUpdate);
};
}, [forceUpdate, initialDims]);
return dims;
} |
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions. |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information. |
Using
useWindowDimensions
causes the app to re-render infinitly.I have no idea why.
I copied the source code to my app and used it without any issues.
Once I use the exported hook this issue resumes.
Running on android.
React Native version: 0.61.2
Steps To Reproduce
useWindowDimensions
Describe what you expected to happen:
use the hook without any issues.
Snack, code example, screenshot, or link to a repository:
The text was updated successfully, but these errors were encountered: