marudy / react-native-responsive-screen

Make React Native views responsive for all devices with the use of 2 simple methods
MIT License
1.55k stars 141 forks source link

Orientation listeners don't work in functional components #65

Open LucasMallmann opened 4 years ago

LucasMallmann commented 4 years ago

I was writing a functional component and I just don't know how to subscribe to the orientation changes. Since the method listenOrientationChange accepts a class instance and use the method useState from class components.

I guess that extending the listener to functional components would be great ;)

AVATAR197 commented 4 years ago

I would like to see that feature as well. I am using hooks and I really don't want to use class-based components.

mshafex commented 4 years ago

im faceing this issue too and my whole app is build with funcational componants please add the support

ahmedjaafar6 commented 4 years ago

same

gregfenton commented 4 years ago

See my pull request: PR https://github.com/marudy/react-native-responsive-screen/pull/83

It has updated docs and a hook example and everything. And the change to RNRS itself is pretty small. And adds a new feature 😃

janpaulalmanoche commented 3 years ago

the error for that.setState is stil here , im also using hooks

tejfaster commented 2 years ago

maybe we can change some code like :

const Orientation = () => { const [dimensions, setDimensions] = useState({ window, screen });

useEffect(() => {
    const subscription = Dimensions.addEventListener(
        'change',
        ({ window, screen }) => {
            setDimensions({ window, screen });
        }
    );
    return () => subscription?.remove();
});
return dimensions.screen.height > dimensions.screen.width ? 'portrait' : 'landscape'

}