【发布时间】:2023-03-03 11:10:02
【问题描述】:
我想把我的对象数据放到标题中。我在 componentWillMount() 中解析对象数据并将其存储在 navigation.setParams(userJSON: object) 中。
我的问题是如果我只是将它打印为 JSON.stringify(object),我可以在标题中看到数据,但是当我想将它显示为 object.name 时出现问题..
我的代码:
componentWillMount() {
const {
setParams
} = this.props.navigation;
AsyncStorage.getItem('user', (err, result) => {
this.setState({
user: JSON.parse(result),
});
var obj = JSON.parse(result);
this.props.navigation.setParams({
userJSON: obj,
});
});
}
标题:
static navigationOptions = ({ navigation }) => ({
headerTitle: "Tab1",
headerLeft: (
<View style={styles.oval}>
<Text>{JSON.stringify(navigation.getParam('userJSON'))}</Text>
</View>
),
headerRight: (
<View style={styles.headerRight}>
<TouchableOpacity
title="Ok"
onPress={() => navigation.navigate('MyProfil')}>
<View style={styles.circle}>
<Text style={styles.txtInside}>
{
navigation.getParam('userJSON').name + ''
+ navigation.getParam('userJSON').lastname
}
</Text>
</View>
</TouchableOpacity>
</View>
)
});
我的对象是:
{
id:86163
lastname:Api
name:Api
email:api@eeee.com
user:api
}
【问题讨论】:
-
试试这个
JSON.parse(navigation.getParam('userJSON')).name -
同样的错误:意外的标识符“未定义” ....:(
标签: javascript react-native react-navigation