【发布时间】:2020-10-01 13:03:54
【问题描述】:
我不知道为什么,但我的应用不喜欢我为组件创建的样式表。在 index.js(顶级)中,我导入 global var
import global from './src/global';
然后我在 /src/styles.js 的 styles.js 文件中使用 global.width 设置为屏幕宽度(正确)。然后,我从 Styles 文件中导出各种样式,例如下面称为“按钮”的样式。最后在我的按钮组件中,我使用数组表示法来响应原生样式,为组件提供多个宽度(假设最高索引优先),但我只是得到一个错误。
导出的“按钮”样式表示例
...
padding: 12,
margin: 10,
//width: global.width/2.0,
alignSelf: "center",
marginTop: "auto",
...
按钮组件
<TouchableOpacity
onPress={this.props.onPress}
style={[
button.regularContainer,
styles.localButtonStyle,
this.props.style,
]}>
<Text style={[
button.regularText,
styles.localTextStyle,
this.props.textStyle,
]}>
{this.props.title || 'Button'}
</Text>
</TouchableOpacity>
错误
Invariant Violation: [33,"RCTView",541,{"color":4278190080,"textAlign":"center","width":"<<NaN>>","backgroundColor":1308622847,"borderWidth":0,"borderColor":4294967295,"marginTop":40}] is not usable as a native method argument
如果我从与组件交互的三个样式表中的大多数中删除宽度元素,这些错误就会消失:button.regularContainer、styles.localButtonStyle、this .props.style
global.js
import React from 'react';
import {
Dimensions,
Platform,
} from 'react-native';
let global: {
HermesInternal: null | {}
};
global.width = Dimensions.get('window').width
global.height = Dimensions.get('window').height
global.ios = Platform.OS === 'ios'
export default global;
【问题讨论】:
标签: react-native