question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Orientation listeners don't work in functional components

See original GitHub issue

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 😉

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:11
  • Comments:6

github_iconTop GitHub Comments

1reaction
gregfentoncommented, Aug 20, 2020

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 😃

0reactions
tejfastercommented, Jul 30, 2022

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'

}

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native: Different styles applied on orientation change
I've made some functions that return the styles object and are called on every component render's function, so I am able to apply...
Read more >
A Comprehensive Guide to Working With Functional ...
The direction React is heading towards is a functional style of coding that makes ... So what are hooks and functional components all...
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
Using functional components as screens | React Native ...
Both class components and functional components can be used as screens. There is no performance difference between the two. Deciding which type component...
Read more >
42-react-native-orientation-locker - NPM Package Overview
When UI orientation changed, callback function will be called. But if lockToXXX is called , callback function will be not called untill ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found