Answer the question
In order to leave comments, you need to log in
Force loading script on React.js, how?
Hello!
Wrote a simple tab script in React.js. According to his idea, he had to open maps on Google Api. However, in order to open another map, apparently, the script must call the Google Api again.
Probably easier to show than to explain.
You can see what is now on Codepen here .
The script loads the first map from the World section. But after trying to open other tabs when changing the div id, the new map does not load.
Advise how it is possible in this case to force React.js to call the script again. Well, or just how to make the whole thing work.
Thank you.
Answer the question
In order to leave comments, you need to log in
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['geochart'], callback: drawVisualization});
function drawVisualization() { ... }
</script>
function drawVisualization(element, commonData, regionData) {
console.log('elemet', element);
var data = google.visualization.arrayToDataTable(commonData);
var mapVisualization = new google.visualization.GeoChart(element);
mapVisualization.draw(data, regionData);
}
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['geochart']});
</script>
class MapVisualization {
componentDidMount() {
this.drawMap();
}
componentDidUpdate() {
this.drawMap();
}
drawMap() {
drawVisualization(this.refs.mapDiv.getDOMNode(), this.props.commonData, this.props.data);
}
render() {
return (
<div ref="mapDiv" />
);
}
};
render() {
var list = this.props.data.map(function(d, i) { return <button type = "button" className = { 'tab' + (i === this.state.current ? ' active' : '') } key = { 'tab-' + i } onClick = { this.handleClick.bind(this, i) }>{d.title}</button>
}.bind(this));
const { commonData, data } = this.props;
const { current } = this.state;
return (
< div className = 'container' >
<div className='col-12'>
{list}
<div className = 'map'>
<MapVisualization data={data[current].content} commonData={commonData} />
</div>
</div>
</div>
);
}
The problem is that at the time of receiving data from the Google api, these elements do not exist, that is, due to the fact that the react and api map components exist independently of each other. I would look towards integrating them with let's say https://github.com/tomchentw/react-google-maps
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question