在 React Native 中过滤和渲染 JSON 响应
我已实现的目标:
通过使用 effect-hook,我使用获取请求来调用 REST api,并使用 state-hook 将该数据保存为 JSON。 如果我想,我可以渲染特定元素。
我已声明状态:
const [apiR, setApiR] = useState([]); /
在我的获取请求中,我将响应转换为 JSON 并保存到状态。
.then((resp) => resp.json())
.then((resp) => setApiR([resp]))
我可以在应用程序中显示数据(显示“TBTC 0.00780001”)
<View>
{apiR.map((item, index) => <Text key={index}> test {item.currencies[index].currency} {item.currencies[index].totalBalance} </Text>)}
</View>
使用
console.log(JSON.stringify(apiR, null, 2))
打印以下内容
[
{
"total": {
"currency": "TBTC",
"totalBalance": "0.00854046",
"available": "0.00854046",
"pending": "0"
},
"currencies": [
{
"active": true,
"currency": "TBTC",
"totalBalance": "0.00780001",
"available": "0.00780001",
"pending": "0",
"btcRate": 1
},
{
"active": false,
"currency": "TETH",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.025145161625394714
},
{
"active": true,
"currency": "TXRP",
"totalBalance": "37.8099",
"available": "37.8099",
"pending": "0",
"btcRate": 0.00001958357570217716
},
{
"active": false,
"currency": "TBCH",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.024926423778461028
},
{
"active": false,
"currency": "TLTC",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.004572634826325412
},
{
"active": false,
"currency": "TZEC",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.005482556714309457
},
{
"active": false,
"currency": "TXMR",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.006804678411168356
}
]
}
]
此时一切正常。如果我重新启动我的应用程序,一切仍将正常工作。 但是,如果我想显示“货币”中“active”= true 的所有结果怎么办?
如果添加代码:
var currencyArray = apiR[0].currencies; //getting the 'currencies' part
var filteredArray = currencyArray.filter(data => data.active); //getting all coins where 'active' = true
console.log(JSON.stringify(filteredArray, null, 2));
目前,它运行良好。
但是,如果我使用此代码重新启动应用程序,我会收到以下错误。
TypeError: undefined is not an object (evaluating 'apiR[0].currencies')
我怀疑这是因为 apiR 默认只是一个空数组,并且来自获取请求的响应数据尚未保存到状态,因为效果挂钩在第一次渲染之后运行 &如果我理解正确的话,每次更新后都会出现这种情况。
如果我删除我添加的最后 3 行代码,并在重新启动应用程序后打印“apiR”,我可以看到,虽然它正确地呈现了我保存到状态的响应中的数据(“TBTC 0.00780001”),但在控制台中它只是说 apiR 是'[]'
我想在启动应用程序时显示过滤后的数据,但不知道如何实现
只需像这样使用它:
var currencyArray = (apiR[0].currencies) ? apiR[0].currencies: [];
我为您提供了一个不使用
useState
钩子的解决方案。假设您有这样的状态:
this.state = {
apiR: []
}
您的
componentDidMount
函数应如下所示:
async componentDidMount() {
await this.setState({
apiR: [
{
"total": {
"currency": "TBTC",
"totalBalance": "0.00854046",
"available": "0.00854046",
"pending": "0"
},
"currencies": [
{
"active": true,
"currency": "TBTC",
"totalBalance": "0.00780001",
"available": "0.00780001",
"pending": "0",
"btcRate": 1
},
{
"active": false,
"currency": "TETH",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.025145161625394714
},
{
"active": true,
"currency": "TXRP",
"totalBalance": "37.8099",
"available": "37.8099",
"pending": "0",
"btcRate": 0.00001958357570217716
},
{
"active": false,
"currency": "TBCH",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.024926423778461028
},
{
"active": false,
"currency": "TLTC",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.004572634826325412
},
{
"active": false,
"currency": "TZEC",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.005482556714309457
},
{
"active": false,
"currency": "TXMR",
"totalBalance": "0",
"available": "0",
"pending": "0",
"btcRate": 0.006804678411168356
}
]
}
]
});
var currencyArray = this.state.apiR[0].currencies; //getting the 'currencies' part
var filteredArray = currencyArray.filter(data => data.active); //getting all coins where 'active' = true
console.log(filteredArray);
}
之后,您将不会收到任何类似
undefined
的错误,并且您的
filteredArray
将在您启动应用时按预期打印。