Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A helper function for React Native which returns values based on screen height
import { Dimensions } from "react-native";
// Height
const screenSizes = [
{
name: "xs",
height: 568
},
{
name: "s",
height: 667
}
];
// Example usage:
// screenSize({xs: 8, s: 12}, 16)
// screenSize({s: 12}, 16)
// screenSize({xs: 8}, 16)
export function screenSize(screenSizeOptions, defaultValue) {
const matchedScreenSizes = screenSizes.filter((screenSize) => {
return Dimensions.get("window").height < screenSize.height;
});
let value;
const hasScreenSizeOption = matchedScreenSizes.some((matchedScreenSize) => {
if (screenSizeOptions.hasOwnProperty(matchedScreenSize.name)) {
value = screenSizeOptions[matchedScreenSize.name];
return true;
} else {
return false;
}
});
if (!hasScreenSizeOption) {
value = defaultValue;
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.