fetching data from mysql but unable to store that data in a state. I'm building user live location. users data is located in mysql and im fetching that data and trying to show data in google map.
import { React, useState, useEffect } from "react";
import "./App.css";
import axios from "axios";
import {
GoogleMap,
withScriptjs,
withGoogleMap,
Marker,
InfoWindow,
} from "react-google-maps";
function Map() {
const [selectedPark, setSelectedPark] = useState([]);
var data = "";
var config = {
method: "get",
url: "http://localhost:3002/user",
headers: {},
data: data,
};
useEffect(() => {
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
setSelectedPark(response.data);
console.log(selectedPark);
})
.catch(function (error) {
console.log(error);
});
}, []);
return (
<GoogleMap
defaultZoom={10}
defaultCenter={{ lat: 24.860966, lng: 66.990501 }}
>
{selectedPark.map((park) => (
<Marker
key={park.user_Id}
position={{
lat: park.latitude,
lng: park.longitude,
}}
onClick={() => {
setSelectedPark(park);
}}
onCloseClick={() => {
setSelectedPark(null);
}}
icon={{
url: "https://raw.githubusercontent.com/leighhalliday/google-maps-react-demo/2bcd4ca7184ea9ed9edbc25021c2e721a74c027f/public/skateboarding.svg",
scaledSize: new window.google.maps.Size(25, 25),
}}
/>
))}
{selectedPark && (
<InfoWindow
position={{
lat: selectedPark.latitude,
lng: selectedPark.longitude,
}}
>
<div>
<h2>{selectedPark.area_name}</h2>
<p>{selectedPark.area_description}</p>
</div>
</InfoWindow>
)}
</GoogleMap>
);
}
const WrappedApp = withScriptjs(withGoogleMap(Map));
function App() {
return (
<div style={{ width: "100vw", height: "100vw" }}>
<WrappedApp
googleMapURL={`https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=AIzaSyDmVpJ44AZEUIs-ml0R1Q0URcmr-Q-DMC4`}
loadingElement={<div style={{ height: "100%" }} />}
containerElement={<div style={{ height: "100%" }} />}
mapElement={<div style={{ height: "100%" }} />}
/>
</div>
);
}
export default App;
server is working perfectly, i can see the data from console, it works fine but the only problem is that im unable to store that data in a state