I used react-native-size-matters.
Problem I'm trying to solve:
Working with % is not always easy if you are styling nested components.
Since devices have different width and height, once your app is styled for a particular device, the app might not have the same look on the other devices.
This is how 370 x 370 square looks on several devices.
const componentStyles = StyleSheet.create({
regularBox: {
width: 300,
height: 300,
backgroundColor: 'cyan',
},
});
As you can see the ratio is different across the devices.
How can we solve this?
I used react-native-size-matters.
rohan@~/prj/rn>> npm i --save react-native-size-matters
rohan@~/prj/rn>> cd ios
rohan@~/prj/rn/ios>> pod install
import {verticalScale, scale} from 'react-native-size-matters';
const size = {
vs40: verticalScale(40), // calculates againt vh
s300: scale(300), // calculates agains vw
};
const componentStyles = StyleSheet.create({
regularBox: {
width: 300,
height: 300,
backgroundColor: 'cyan',
},
sizedBox: {
width: size.s300,
height: size.s300,
backgroundColor: 'cyan',
},
});
This makes development easy because we don't have to open multiple simulators and test how it looks on different devices.
If your implementation is aligning with the design in a iPhone X Simulator it will look the same on the other devices as well.
Top comments (1)
wow thanks, I had a heard time dealing with the responsiveness, I hope react native size matters will help.