gpt4 book ai didi

javascript - 如何将从 api 获取的一些单词而不是 'true' 或 'false' 值渲染到 FlatList 中?

转载 作者:行者123 更新时间:2023-12-02 23:29:44 25 4
gpt4 key购买 nike

我能够从 API 获取许多数据的“true”或“false”值数组。如果值为“true”,我如何呈现“Hello”,否则对于所有获取的数据,如何呈现“Good Night”。我使用 String(item.attributes.status) 来显示 true 或 false 值。我有一个 API,其中包含许多带有 bool 值“状态”等属性的数据。

<FlatList

ItemSeparatorComponent={this.FlatListItemSeparator}

data={this.state.companies}
renderItem={({item, index, separators}) => (

<View>
<Text style={styles.listText}>{item.attributes.status}</Text>

</View>

)}

keyExtractor={(item, index) => index.toString()}

/>

getCompanies = () => {
console.log('getCompanies function');

this.setState({ loading: true }, () =>
{
fetch('https://------------------------------------',
{
method: 'GET',
headers:
{
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': '------------------------------------------------',
},

})
.then(response => {
console.log('response');
return response.json()
this.setState({ loading: false});

})
.then(RetrivedData => {
// Work with JSON data here
console.log('Retrived Data');

if(RetrivedData.success){

console.log('Success');
this.setState({
companies: RetrivedData.data
});

}
else{
Alert.alert('Error !!!!!!!!');
}
this.setState({ loading: false});

})
.catch(err => {
// Do something for an error here
this.setState({ loading: false});
});
});
};

最佳答案

采用 if 的简写形式。这:

true ? 'True' : 'False' 

将返回“True”,如下所示:

if (true) {return 'True'}; else {return 'False'};

尝试以这种方式编写文本:

<Text style={styles.listText}>{item.attributes.status ? 'Hello' : 'Good Night'}</Text>

关于javascript - 如何将从 api 获取的一些单词而不是 'true' 或 'false' 值渲染到 FlatList 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56568052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com