Answer the question
In order to leave comments, you need to log in
How to initialize yandex map in react component along with page loading?
Good afternoon, please push the truth. there is a main component and there is a component with a map
class MainComponent extends React.Component {
}
class MapComponent extends React.Component {
handleScriptLoad = () => {
const {ymaps} = window;
ymaps.ready(this.init);
}
init = () => {
const {ymaps} = window;
const myMap = new ymaps.Map('map', {
center: [55.76, 37.64],
zoom: 5,
controls: []
});
return myMap;
}
render() {
const instanceMap = this.init();
return (
<div>
<Script
url="https://api-maps.yandex.ru/2.1/?&lang=ru_RU"
onLoad={this.handleScriptLoad}
/>
<button onClick={() => какая-то функция(instanceMap)}>Add</button>
<div id='map' className={styles.containerMap}></div>
</div>
)
}
}
Answer the question
In order to leave comments, you need to log in
Use the componentDidMount() lifecycle and make a map call in it
https://metanit.com/web/react/2.6.php
ps: I'm not sure about the correct connection of the map in your code.
I suggest you check out a similar question. How to work with yandex map React?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question