Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 11319a4

Browse files
committed
create responsive hooks
1 parent e700483 commit 11319a4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25+
import { useState, useEffect } from "react";
2526
import { Dimensions } from "react-native";
2627

2728
export const responsiveHeight = h => {
@@ -39,3 +40,56 @@ export const responsiveFontSize = f => {
3940
const tempHeight = (16 / 9) * width;
4041
return Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(width, 2)) * (f / 100);
4142
};
43+
44+
export const useResponsiveHeight = h => {
45+
const [calculatedHeight, setCalculatedHeight] = useState(responsiveHeight(h));
46+
47+
useEffect(() => {
48+
function handleDimensionChange() {
49+
setCalculatedHeight(responsiveHeight(h));
50+
}
51+
52+
Dimensions.addEventListener("change", handleDimensionChange);
53+
return () => {
54+
Dimensions.removeEventListener("change", handleDimensionChange);
55+
};
56+
});
57+
58+
return calculatedHeight;
59+
};
60+
61+
export const useResponsiveWidth = w => {
62+
const [calculatedWidth, setCalculatedWidth] = useState(responsiveWidth(w));
63+
64+
useEffect(() => {
65+
function handleDimensionChange() {
66+
setCalculatedWidth(responsiveWidth(w));
67+
}
68+
69+
Dimensions.addEventListener("change", handleDimensionChange);
70+
return () => {
71+
Dimensions.removeEventListener("change", handleDimensionChange);
72+
};
73+
});
74+
75+
return calculatedWidth;
76+
};
77+
78+
export const useResponsiveFontSize = f => {
79+
const [calculatedFontSize, setCalculatedFontSize] = useState(
80+
responsiveFontSize(f)
81+
);
82+
83+
useEffect(() => {
84+
function handleDimensionChange() {
85+
setCalculatedFontSize(responsiveFontSize(f));
86+
}
87+
88+
Dimensions.addEventListener("change", handleDimensionChange);
89+
return () => {
90+
Dimensions.removeEventListener("change", handleDimensionChange);
91+
};
92+
});
93+
94+
return calculatedFontSize;
95+
};

0 commit comments

Comments
 (0)