我的样式对象:mainModule / styles.js export default StyleSheet.create({ container: { width: 0 }, basicInfo: { height: 167, backgroundColor: 'red, justifyContent: 'center', alignItems: 'center' }} 当我从’@ mainModule / styles’导
export default StyleSheet.create({
container: {
width: 0
},
basicInfo: {
height: 167,
backgroundColor: 'red,
justifyContent: 'center',
alignItems: 'center'
}
}
当我从’@ mainModule / styles’导入import generalStyle时(我创建了一个package.json文件以使该路径有效)
控制台日志显示如下:
对象{
容器:10,
basicInfo:118
}
这里的任何人都可以帮助我吗?
@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
