I am using ReactJS, Ethers.js, and solidity.
Here, I'm using a useEffect to retrieve data from a smart contract (using ethers.js). This works fine.
const createAddressArray = async () => {
const addressArr = await contract.getArray()
setAddressArray(addressArr);
const cArr = [];
addressArr.map( async (a) => {
const data = await contract.getSample(a)
const lat = parseFloat(data.latitude);
const lng = parseFloat(data.longitude);
const l = parseInt(data.somethingelse.toHexString(), 16);
const message = data.message;
const address = data.sampleAddress;
cArr.push([lat, lng, l, address, message])
})
setCirclesArr(cArr);
console.log(cArr) // everything here works fine
};
createAddressArray()
}, [])
However, when I call this in the html section, it doesn't work. Perhaps it's because it's running before the circlesArr state is set, but once that's set it should have rerendered right? Apparently not.
<div>
{circlesArr}
{circlesArr.map(() => {
console.log('mapping')
})}
</div>
The circlesArr does not show up in the div, and "mapping" does not show up in the console, suggesting that the array is empty when this runs. How do I make this run again when the array is updated? Thanks in advance!