我想根据服务器返回的响应更改按钮文本。怎么可能? 我的回答看起来像这样
{
"responseHeader": {
"type": "314",
"status": "200",
"message": "Successfully found the profile"
},
"neighbourProfile": [{
"no_of_mutual_friends": "0",
"is_anchored": "1",
},
{
"no_of_mutual_friends": "0",
"is_anchored": "0"
}]
}
我想根据状态is_anchored
更改按钮文字,如果是0,我希望按钮文本为friends
,否则为add
,我该怎么做?这是我的反应部分
return (
<Card>
<CardSection>
<Image style ={styles.thumbnail_style} source={{uri : profile_image_url}} />
<View style= {styles.thumbnailContainerStyle}>
<Text style={styles.userStyle}>{username}</Text>
<Image source={require('./src/Images/profession_cell.png')}/><Text style={styles.textStyle}>
No profession to show</Text>
<Text style={styles.friendStyle}>{no_of_mutual_friends} Mutual friends</Text>
</View>
<View style={styles.buttonStyle}><Button color="#00BFA5" title="Add" onPress={() =>console.log("clicked")}>
</Button></View>
</CardSection>
</Card>
);
答案 0 :(得分:3)
使用上一个问题,在州中设置邻居之后:
.then((responseData) =>this.setState({neighbours: responseData.neighbourProfile}));
您可以像这样渲染按钮:
render() {
return (
<View>
{
this.state.neighbours.map( (neighbour) => {
return (
<Button title={neighbour.is_anchored == "0" ? 'friends' : 'add'} />
)
})
}
</View>
);
}
这将显示简单的结果反映到邻居的is_anchored
值。
更新1:
随着您的问题更新,似乎应该有一个变量is_anchored
。然后它可能是:
<Button color="#00BFA5" title={is_anchored == "0" ? 'friends' : 'add'} onPress={() =>console.log("clicked")} />