I am trying to add the rotation functionality on react native using expo. Followed tutorials and searched on google and here but somehow none of them are working. The screen size is not getting detected when i rotate the simulator. Therefore it doesn't change from portrait to landscape or vice versa. You can see the code below that i tried through following a working example but its not working when i apply it. Any help is very much appreciated.
const [orientation, setOrientation] = useState("");
const window = useWindowDimensions();
useEffect(() => {
Dimensions.addEventListener("change", ({ window: { width, height } }) => {
if (width < height) {
// this is in portrait mode
setOrientation("Portrait");
} else {
// this is in landscape mode
setOrientation("Landscape");
}
});
}, []);
return (
<View style={styles.container}>
<Text style={{ color: "red", fontSize: 30, fontWeight: "bold" }}>
{orientation}
</Text>
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
});

