What is the default unit of style in React Native? What is the default unit of style in React Native? android android

What is the default unit of style in React Native?


From the docs:

All dimensions in React Native are unitless, and represent density-independent pixels. Setting dimensions this way is common for components that should always render at exactly the same size, regardless of screen dimensions.

So yes, units in React Native are in dp. If you want to convert them to pixels, use PixelRatio.getPixelSizeForLayoutSize()


I share your confusion somewhat, not being able to actively inspect with a developer console as we are used to in the browser.

I am not familiar with the 'dp' unit, but from what I gather width: 1 renders differently on each device depending on the pixel density of the screen (see link). The information in the react-native docs say that 1 would render thicker on screens with high pixel density. Which then sounds logical as you have more precision on high density screens than you would have on low density screens and react-native aims at being universal so it would not assume high dpi.

It is my understanding that you can use the below linked PixelRatio API to calculate sizes for detail elements (think borders, icons, etc), that way you can dynamically adjust the rendered size according to the device's screen density.

https://facebook.github.io/react-native/docs/pixelratio.html#content


It is the pixel ratio that you have to consider. pixel represents an absolute value. pixel ratio is a relative value. to make app screen and components responsive you have to use pixel ratio.

i have been using in multiple apps already. and i think that is how you have to do it. hope this answers your question.