Reputation: 99
I use OpenLayers API Map in my project. I have no idea what is the correct way to use js code in react or vice verca?
Here is answer how to user markers in map, but I can't use it in my current code as I don't understand react sytax as well. Here is my code example with empty map.
import React, { Component } from 'react';
import { Map, layer, Layers } from 'react-openlayers';
import { Container } from 'react-bootstrap';
import { transform } from 'ol/proj';
import '../../styles/openlayers-map.css';
class OpenlayersMap extends Component {
render() {
return (
<Container className="map-container">
<Map view={{
center: transform([30, 50],
'EPSG:4326', 'EPSG:3857'),
zoom: 12,
maxZoom: 18,
minZoom: 4,
}}
>
<Layers>
<layer.Tile />
</Layers>
</Map>
</Container>
);
}
}
export default OpenlayersMap;
There is a sample of working js code provided Here. I will really appreciate for any help to use this code for my purpose
Upvotes: 0
Views: 151
Reputation: 10021
react is just a library that handles when DOM nodes get updated, the rest is plain javascript. So, when working with react, the 3 most common things you touch that are react-specific are:
therefore, outside the JSX elements, you can do any javascript you want:
class MyComponent extends React.Component {
// 1
state = {
mountOffset: 0,
};
// 2
componentDidMount() {
// 3
this.initMountOffsetInterval();
}
// 4
initMountOffsetInterval() {
// 5
setInterval(() => {
// 6
const mountOffset = this.state.mountOffset + 1;
// 7
this.setState({ mountOffset });
}, 1000);
}
// 8
render() {
// 9
const { mountOffset } = this.state;
// 10
return (
<h1>
This component has been mounted for: { mountOffset } seconds
</h1>
);
}
}
let's break down the example above; theres a mix of plain javascript and react-specific code:
Upvotes: 2