React Native Layout Provider
Getting layout size by top provider instead of Dimensions
. This is split from react-native-layout-tester's context & getLayout
decorator. Inspired by Provider / connect of react-redux.
Why?
Originally react-native-layout-tester
have a problem for Dimensions
, you can't get a correct layout with Dimensions for each component if you're using LayouTester wrapper, so we made a getLayout
decorator to resolve it.
It's mean you must be use getLayout
instead of Dimensions
for each component, but you can keep the same code in production.
Installation
$ npm install --save react-native-layout-provider
Usage
Use LayoutProvider
to wrap your app container:
;; <LayoutProvider // Default: "Default" label=String // Default: provided by Dimensions width=Number height=Number // Default: undefined, you can use react-native-orientation to get portrait portrait=Boolean > <YourApp /> </LayoutProvider>;
And use getLayout
for each component, the component props will update when LayoutProvider props changed:
;; // It's same with defaultLayoutTypes static propTypes = label: PropTypesstringisRequired viewport: PropTypesshape width: PropTypesnumberisRequired height: PropTypesnumberisRequired isRequired portrait: PropTypesbool ; { return ...; } label: layoutlabel viewport: width: layoutviewportwidth height: layoutviewportheight portrait: layoutportraitComp;
Or use babel-plugin-transform-decorators-legacy to get @getLayout()
work.
Advanced example
Use react-native-orientation to get / listen orientation, and react-native-extra-dimensions-android to resolved status bar problem for Android.
;;;;; let statusBarHeight = 0;if PlatformOS === 'android' try statusBarHeight = ExtraDimensions; catche {} state = orientation: Orientation ; { this; }; { Orientation; } { Orientation; } { const height = Dimensions; return <LayoutProvider label="Default" height=height - statusBarHeight // Resolved statusBar problem for Android portrait=thisstateorientation === 'PORTRAIT' > ... </LayoutProvider> ; }