I have recently begun using react native and I'm building this mobile application using Expo. I am using useEffect() to be able to call a function inside it. All I want is to wait for the reponse of the API to finish completely before I display the result (which in this case is the connectionName variable). I gave the useEffect() function walletsas a second parameter because I want the API fetched again whenever wallets changes, but I seem to be running in an infinite loop even though wallets hasn't changed. Any help is much appreciated.
export default function LinksScreen() {
const [wallets, setWallets] = React.useState([]);
const [count, setCount] = React.useState(0);
const [connectionName, setConnectionName] = React.useState("loading...");
React.useEffect(() => {
(async () => {
fetchConnections();
})();
}, [wallets]);
async function fetchConnections() {
const res = await fetch('https://api.streetcred.id/custodian/v1/api/' + walletID + '/connections', {
method: 'GET',
headers: {
Accept: 'application/json',
},
});
res.json().then(res => setWallets(res)).then(setConnectionName(wallets[0].name))
}
return (
<ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}>
<OptionButton
icon="md-school"
label={connectionName}
onPress={() => WebBrowser.openBrowserAsync('https://docs.expo.io')}
/>
</ScrollView>
);
}