Я пытаюсь вывести содержимое JSON в плоский список, как описано в собственных документах реагирования. Вывод содержимого на страницу - это не проблема, с которой я сталкиваюсь, я пытаюсь ограничить объем данных, выводимых из каждого поля JSON.
Например, есть 4 состояния: физическое, психическое, эмоциональное и духовное. Я пытаюсь вывести только первый элемент, поэтому в данном случае физический, или [0].
У меня есть следующий код, который выведет все 4 значения, и у меня возникли проблемы с ограничением значений только первым элементом:
<FlatList
data={this.state.dimensionJson}
renderItem={({item}) => <Text style={[styles.dimensionTitle, { color: progress[3] }]}>{item.type}</Text>}
keyExtractor={({id}, index) => id}
/>
код, который извлекает данные JSON, выглядит следующим образом:
componentDidMount(){
return fetch(url,{
method: 'GET',
headers: {
Accept: 'applications/json',
},
},
)
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
titleData: responseJson.title,
fullJSON: responseJson,
dimensionJson: responseJson.dimensions,
}, function() {
//Potentially write if function in here for limiting output
});
})
.catch((error) =>{
console.error(error)
})}
и, наконец, мой JSON выглядит так:
{
"description": "Begin by identifying the dimension of energy you would like to address. Your scores can guide the way.",
"title": "Choose a Dimension",
"dimensions": [
{
"id": "0",
"type": "Physical",
"desc": "Physical energy is the quantity of energy. This dimension shapes our sustainability and long-term productivity."
},
{
"id": "1",
"type": "Mental",
"desc": "Mental energy is the focus of our energy. It influences our
concentration, control of attention, and the likelihood of making mistakes."
},
{
"id": "2",
"type": "Emotional",
"desc": "Emotional energy is the quality of our energy. It affects how
resilient we are, especially when faced with complexity."
},
{
"id": "3",
"type": "Spiritual",
"desc": "Spiritual energy is the energy we derive from serving a
greater purpose. It inspires us and answers the question ‘Why do I get out
of bed each morning?"
}
]
}