react-native-extra-dimensions-android
react-native-extra-dimensions-android copied to clipboard
Is this package really not needed anymore? (And why?)
React-native-modal still references to use this package in their ReadMe
@JeffreyVanelderenACA Thanks for letting me know. I'll look at testing this on the latest RN version, but likely in the New Year. I've a lot of different on-going projects that require my attention.
I had the same feeling as @JeffreyVanelderenACA, I was looking for an alternative but could not find any.
Any news?
I found a solution that seemed to work pefectly for me here: https://github.com/facebook/react-native/issues/23693#issuecomment-1207483253
It essentially boils down to - load a full-size view component at your app root, then get the dimensions from the onLayout
prop's call argument e.nativeEvent.layout
.
Here's a minimal TypeScript hook for ease of use:
import React, { useContext, useState } from "react";
import { Dimensions, LayoutRectangle, View } from "react-native";
type LayoutSize = Pick<LayoutRectangle, "width" | "height">;
const ScreenDimensionsContext = React.createContext<LayoutSize | null>(null);
export const useActualDimensions = () => {
const dimensions = useContext(ScreenDimensionsContext);
if (!dimensions) throw new Error("useDimensions hook must be used inside provider");
return dimensions;
};
interface ScreenDimensionsProviderProps {
children: React.ReactNode;
}
export function ActualDimensionsProvider({ children }: ScreenDimensionsProviderProps) {
const [dimensions, setDimensions] = useState<LayoutSize>(Dimensions.get("window"));
return (
<ScreenDimensionsContext.Provider value={dimensions}>
<View style={{flex: 1}} onLayout={(e) => setDimensions(e.nativeEvent.layout)}>
{children}
</View>
</ScreenDimensionsContext.Provider>
);
}