reactjs - react native : All styles in StyleSheet object after exporting were converted to number type

标签 reactjs react-native

我的样式对象:mainModule/styles.js

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

当我导入 import GeneralStyle from '@mainModule/styles' 时(我创建了一个 package.json 文件以使该路径有效) 控制台日志显示如下:

对象{ 容器:10个, 基本信息:118 }

这里有人可以帮助我吗?

最佳答案

@NellyNgo,您可能试图展平 StyleSheet.create 返回的整个对象。我做了同样的事情,这让我发疯,因为它似乎对其他人都有效,但对我来说返回了我传递给它的相同值。实际上,展平适用于由 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

关于reactjs - react native : All styles in StyleSheet object after exporting were converted to number type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43604807/

相关文章:

reactjs - "To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function."错误但我没有使用它

javascript - 如何在 event.target-Js/ReactJs 上添加类名

javascript - Firebase 实时数据库 - "Error: Client is offline"。 react /NextJS

android - 您的项目的 SDK 版本 >= 33.0.0,但 expo 包版本似乎更旧

android - 我可以使用 Amazon fire 平板电脑来测试我的 android react native 应用程序吗?

javascript - PanResponder 在第二次拖动时将 Animated.View 捕捉回原始位置

javascript - 如何在 React 中设置状态来更新数组?

reactjs - 根据显示的页面更新标题标题(同级组件)

javascript - 检测用户是否连接到互联网?

ios - React Native 应用程序在 Debug模式下工作,但在 iOS 上不工作 Release模式