对象作为React子无效(反应导航)
问题描述:
我正在使用React Navigation
,我想在StackNavigator
中添加右键。加入右键后,我得到这样的错误:对象作为React子无效(反应导航)
Object is not valid as React child (found: object with keys {right}). If you meant to render a collection of children, use an array instead
如何解决这个错误?
下面是代码:
import React, {Component} from 'react';
import {View, Text} from 'react-native';
class ReviewScreen extends Component {
static navigationOptions = {
title: 'Review Jobs',
header:() => {
return {
right: <Text>Go right</Text>
};
}
}
render(){
return (
<View>
<Text>ReviewScreen</Text>
</View>
);
}
}
export default ReviewScreen;
答
不要使用header
。 header
需要表示整个标题的React组件,而不是普通对象。使用headerRight
:
static navigationOptions = {
title: 'Review Jobs',
headerRight: <Text>Go right</Text>
}
答
你不能在头选项渲染的对象。您正在返回一个对象而不是文本标记。只要改变你的代码如下,使其工作: -
headerRight:<Text>Go right</Text>
代替: -
header:() => {
return {
right: <Text>Go right</Text>
};
}
答
您可以使用headerRight
道具你的头的右边的按钮设置为自定义一个。
例
static navigationOptions = {
title: 'Review Jobs',
headerRight:<Text>Go right</Text>
}