react-native-extra-dimensions-android icon indicating copy to clipboard operation
react-native-extra-dimensions-android copied to clipboard

Is this package really not needed anymore? (And why?)

Open JeffreyVanelderenACA opened this issue 2 years ago • 4 comments

React-native-modal still references to use this package in their ReadMe

JeffreyVanelderenACA avatar Nov 09 '22 10:11 JeffreyVanelderenACA

@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.

Sunhat avatar Dec 13 '22 15:12 Sunhat

I had the same feeling as @JeffreyVanelderenACA, I was looking for an alternative but could not find any.

KevinvdBurg avatar Mar 15 '23 10:03 KevinvdBurg

Any news?

lyswhut avatar Mar 21 '23 07:03 lyswhut

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>
  );
}

finnmerlett avatar Jul 13 '23 05:07 finnmerlett