React Native: all styles in the StyleSheet object were converted to type number after export

My style object: mainModule/styles.js

export default StyleSheet.create({
  container: {
    width: 0
  },
  basicInfo: {
    height: 167,
    backgroundColor: 'red,
    justifyContent: 'center',
    alignItems: 'center'
  }
}

When I import import generalStyle from '@mainModule/styles'(I created a file package.jsonso this path works) And the console log looks like this:

Object {container: 10, basicInfo: 118}

Can anyone here help me?

+4
source share
4 answers

@NellyNgo, , , , StyleSheet.create. , , , , . flatten , StyleSheet.create. :

const styles = StyleSheet.create({prop: {...}, prop2: {...})

StyleSheet.flatten(styles.prop)

node_modules/react-native/Libraries/StyleSheet/flattenStyle.js. , .

 function getStyle(style) {
          if (typeof style === 'number') {
            return ReactNativePropRegistry.getByID(style);
          }
          return style;
     }

RN 0.41.2

+2

StyleSheet.create , , , .

, , , StyleSheet.create.

+1

In styles.js :

const stylesObj = {
  container: {
    width: 0
  },
  basicInfo: {
    height: 167,
    backgroundColor: 'red',
    justifyContent: 'center',
    alignItems: 'center'
  }
}

export default stylesObj

And in the component:

import stylesObj from 'styles'

const styles = StyleSheet.create(stylesObj)
+1
source
export default YourStyle = StyleSheet.create({
  container: {
    width: 0
  },
  basicInfo: {
    height: 167,
    backgroundColor: 'red,
    justifyContent: 'center',
    alignItems: 'center'
  }
}

then import YourStyle from './YourStyle';

0
source

Source: https://habr.com/ru/post/1675591/


All Articles