道具类型失败:提供给“视图”的props.style无效键'textAlign'

时间:2018-09-21 11:27:45

标签: reactjs react-native

我是react-native的新手,我得到了这个警告:

 Warning:Failed prop type: Invalid props.style key 'textAlign' supplied to 'View'
 Bad object:{
             "textAlign":"center",....}

可能是问题所在,应用没有崩溃,但我只是想知道如何解决此警告...

这是堆栈跟踪后的代码:

 <View style={styles.botTextView}>
 .....
 botTextView: {
textAlign: 'center',
position: 'relative',
flex: 1,
top: 8,
flexDirection: 'column',
alignItems: 'center',
alignSelf: 'center',
justifyContent: 'center',
 }

谢谢

1 个答案:

答案 0 :(得分:2)

如果您查看React-native文档:https://facebook.github.io/react-native/docs/view-style-props

您将看到视图组件没有textAlign样式道具。 如果要在本机应用程序上显示文本,请考虑使用“文本”组件。

编辑:

由于这两行,您的文本居中:

alignItems: 'center'       //centers the View's children on the y-axis
justifyContent: 'center'   //centers the View's children on the x-axis

但是,如果您想使文本在x轴上居中而不使View的其他子项居中,则可以尝试以下操作:

<View style={styles.botView}>
    <Text style={styles.botText}>
        //Text
    </Text>
</View>
.....
botView: {
    position: 'relative',
    flex: 1,
    top: 8,
    flexDirection: 'column',
    alignItems: 'center',
},
botText: {
    textAlign: 'center',
},