首页 > 解决方案 > React Native 样式问题。不变违例

问题描述

本机反应中,我正在使用以下样式设置组件

const style = StyleSheet.create({
    height: 100,
    borderBottomWidth: 1,
    borderBottomColor: "rgb(201, 204, 204)"
})

但它给出了错误:

在此处输入图像描述

这似乎borderBottomColor是一个有效的属性。我找不到错误的原因。

如果直接添加样式。即,没有StyleSheet.create那么一切都运行完美,风格也得到应用

const style = {
    height: 100,
    borderBottomWidth: 1,
    borderBottomColor: "rgb(201, 204, 204)"
}

是否建议在 react native 中直接使用样式?

标签: androidreactjsreact-native

解决方案


您的使用StyleSheet.create不太正确。尝试:

const styles = StyleSheet.create({
    foo : {
        height: 100,
        borderBottomWidth: 1,
        borderBottomColor: "rgb(201, 204, 204)"
    }

})

然后将其引用为styles.foo,如:

<View style={styles.foo} />

您还可以像这样组合样式表和内联样式:

<View style={[styles.foo,{backgroundColor:'green'}]}/>

最后,样式表可以有多个命名样式,例如:

const styles = StyleSheet.create({
    foo : {
        height: 100,
        borderBottomWidth: 1,
        borderBottomColor: "rgb(201, 204, 204)"
    },
    bar : {
        width:50
    }

})

推荐阅读