Trying to work with openlayers,reactjs and golden-layout. Golden-layout configuration is one row with two "react-components" inside it. Openlayers does render map in one "react-component" of golden-layout but not in other "react-component".
Here is code i am trying.
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
// import TestComponent from './App';
import GoldenLayout from "golden-layout";
import * as serviceWorker from './serviceWorker';
import "golden-layout/src/css/goldenlayout-base.css";
import "golden-layout/src/css/goldenlayout-dark-theme.css";
import "ol/ol.css";
window.React = React;
window.ReactDOM = ReactDOM;
var map = new Map({
layers: [
new TileLayer({
source: new XYZ({
url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
})
})
],
view: new View({
center: [0, 0],
zoom: 2
})
});
class TestComponent extends React.Component{
componentDidMount() {
map.setTarget("map")
}
render() {
return (<div id="map"></div>)
}
}
var myLayout = new GoldenLayout({
content: [
{
type: 'row',
content:[
{
type:'react-component',
component: 'test-component',
},
{
type:'react-component',
component: 'test-component',
}
]
}
]
});
myLayout.registerComponent( 'test-component', TestComponent);
myLayout.init();
Here is result. openlayers with reactjs and golden-layout
Need two instances of openlayers map and also element with unique
id
.if only one instance of map used, then changing it's target element will not render map in both elements.
So need two instances of map, with unique element
id
.Also golden-layout renders the provided react component in it's layout/reat-component which means you need to set the
id
incomponentDidMount()
function of reactjs component life cycle. componentDidMountHere is code that work.