microsoft / react-native-macos

A framework for building native macOS apps with React.
https://microsoft.github.io/react-native-windows/
MIT License
3.39k stars 130 forks source link

useWindowDimensions causes infinite re-rendering #2083

Closed bear-ei closed 5 months ago

bear-ei commented 5 months ago

Environment

react-native -v: 0.72.9
npm ls react-native-macos: 0.72.13
node -v: 21.4.0
npm -v: 10.2.4
yarn --version: 4.0.2
xcodebuild -version: Xcode 15.2 Build version 15C500b

Steps to reproduce the bug

Try to drag and drop to change the application window size after using useWindowDimensions.

export const Tooltip: FC<TooltipBaseProps> = () => {
    const windowDimensions = useWindowDimensions();

    return (
        <View>
            <Text>{windowDimensions.width}</Text>
        </View>
    );
};

Expected Behavior

No response

Actual Behavior

No response

Reproducible Demo

No response

Additional context

▶︎ 'Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn\'t have a dependency array, or one of the dependencies changes on every render.',

Saadnajmi commented 5 months ago

I don't repro this with RNTester on either 0.72 or 0.73. Are you sure there isn't an extra setState somewhere in your app?

https://github.com/microsoft/react-native-macos/assets/6722175/6f2e1a22-3c5d-485f-854c-36745992cfcb

bear-ei commented 5 months ago

I don't repro this with RNTester on either 0.72 or 0.73. Are you sure there isn't an extra setState somewhere in your app?

Screen.Recording.2024-02-22.at.11.06.40.PM.mov

Thanks for your reply, my situation is a bit complicated, I'm building a reusable UI component and just introduced useWindowDimensions() , the component doesn't depend on it for the behavior of setState then it shows "Maximum update depth exceeded" and the error stack points to setState in useWindowDimensions, I think I need to double-check the problem.

Saadnajmi commented 5 months ago

FWIW, React Native macOS also just supports setting the tooltip prop on any View/Text/Image.