UNPKG

rlayers

Version:

React Components for OpenLayers

58 lines 1.99 kB
import React from 'react'; import { RContext } from '../context'; import debug from '../debug'; /** * An abstract class used as base for all Style components, not meant to be used directly */ export default class RBaseStyle extends React.PureComponent { constructor(props, context) { super(props, context); if (!this.context) throw new Error('A style property must be part of a style'); this.ol = this.create(props); } /* istanbul ignore next */ create(props) { throw new Error('RBaseStyle is an abstract class'); } refresh(prevProps) { debug('refreshStyle', this); if (!prevProps) return; for (const p of this.classProps) { const m = p.charAt(0).toUpperCase() + p.substring(1); if ((prevProps && prevProps[p]) !== this.props[p]) { if (this.ol['set' + m]) { this.ol['set' + m](this.props[p]); } else { // eslint-disable-next-line no-console console.error(`Underlying OpenLayers object does not support updating of ${p} after object creation. ` + 'If you are using an anonymous constant array or object, ' + 'consider assigning its value to a constant and then passing the constant or ' + 'use React.useMemo() to avoid this warning and improve performance.'); } } } } /* istanbul ignore next */ set(ol) { return; } componentDidMount() { this.set(this.ol); } componentDidUpdate(prevProps, prevState, snapshot) { if (prevProps !== this.props) this.refresh(prevProps); } componentWillUnmount() { this.set(null); } render() { return null; } } RBaseStyle.contextType = RContext; RBaseStyle.classProps = []; //# sourceMappingURL=RBaseStyle.js.map