You need to use the geodata in the tag, in the center attribute. But React prohibits the use of js code in a block with html. I tried to pull data from promise, but I always got undefined.
function App() {
var lc;
var getLocationPromise = new Promise((resolve) => {
navigator.geolocation.getCurrentPosition(async function (position) {
resolve([position.coords.latitude, position.coords.longitude])
})
})
getLocationPromise.then((location) => {
lc = location
return lc
}).catch((err) => {
document.white("error")
})
return (
<YMaps>
<Map className="myMap" defaultState={{ center: [34, 55], zoom: 9 }} ></Map>
</YMaps>
</div>
)
}
export default App;
2
Answers
You may use react local state as shown below
Did you try useEffect hook ?
You should be able to reference the object from anywhere (if thats what you want to do) with React’s "useEffect"and "useState" properties.
For your usecase, you should do something like this:
and then in your code, you can access the geoData and use it as you want.