I'm using useEffect to make 2 requests to 2 different API's. I'm building an array based on the info that's getting returned. I'd like to access this array outside of useEffect and use it in the return below, where I want to use the data to render points on a map. When I try to access it, like how I'm using parkData it says all_data is not defined.
import React, {useEffect} from "react";
import {MapContainer, Marker, TileLayer } from "react-leaflet";
import * as parkData from "./data/skateboard-parks.json";
import "./App.css";
import axios from 'axios';
let all_info = []
export default function App() {
const validator_url = "http://api-1.com"
const ip_url = "http://ip-api.com/json/"
useEffect(() => {
async function fetchData() {
const result1 = await axios.get(validator_url);
for (let i = 0; i < result1.data.count; i+=1) {
const result2 = await axios.get(`${ip_url}${result1.data.results[i].ip_address}`);
let ip_address = result1.data.results[i].ip_address
let lat = result2.data.lat
let lon = result2.data.lon
all_info.push([ip_address, lat, lon])
}
}
fetchData();
}, []);
return (
<MapContainer center={[45.4, -75.7]} zoom={12}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{/* // HOW CAN I ACCESS all_info ARRAY HERE instead of using parkData? */}
{parkData.features.map(park => (
<Marker key={park.properties.PARK_ID} position={[park.geometry.coordinates[1], park.geometry.coordinates[0]]}>
</Marker>
))}
</MapContainer>
);
}
fetchDatafucntion...