What i have achieved:
By using a effect-hook i'm using a fetch request to call a REST api and saving that data as JSON using a state-hook. I can render a specific element if i want to.
I've declared state:
const [apiR, setApiR] = useState([]); /
In my fetch request, i convert response to JSON and save to state.
.then((resp) => resp.json())
.then((resp) => setApiR([resp]))
i can display the data in app (this displays "TBTC 0.00780001")
<View>
{apiR.map((item, index) => <Text key={index}> test {item.currencies[index].currency} {item.currencies[index].totalBalance} </Text>)}
</View>
using console.log(JSON.stringify(apiR, null, 2)) prints the following
[
{
"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
}
]
}
]
At this point all works well. If i were to restart my app everything will still work fine. But what if i want to show all results in 'currencies' where 'active'= true?
If add the code:
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));
It works good, for now.
But if i restart app with this code i get the following error.
TypeError: undefined is not an object (evaluating 'apiR[0].currencies')
I suspect it's because apiR is just an empty array by default and the response data from the fetch request haven't been saved to state yet, because effect hooks run after the first render & after every update if i understood correctly.
If i remove the last 3 lines of code i added and instead print "apiR" after restarting the app, i can see that while it correctly renders the data from the response i saved to state("TBTC 0.00780001"), in console it just says that apiR is '[]'
I want to display filtered data when i start app, but don't know how to implement this
'active' = truewhen you start the app or you can't see data in view mode when you start the app with the existing code?var currencyArray = apiR[0].currenciesi getTypeError: undefined is not an object (evaluating 'apiR[0].currencies')