We're currently working on a new web mapping solution at our company. So far we decided to build the app using React
and OpenLayers 4
. Since we want to use the Redux
pattern for our architecture there will be one redux store holding the application state.
The problem we face with this stack is as follows:
The map is the central element in our application and its instance needs to be passed to a number of different components. As an example, a tool for drawing features on the map needs a reference to the map
instance so that it can add itself to it as an interaction tool.
We discussed how to structure our app to integrate OpenLayers
with React
in the most reliable way and ended up with two different approaches:
The first approach we discussed is holding a reference to the map object in the application-wide redux store so that it simply can get passed down to any component via the
@connect
annotation function ofreact-redux
. While this solution provides an easy access tomap
we were wondering whether this would be a tractable approach since the store should be kept minimal and themap
object never changes throughout the lifecycle of the application.The second approach considers rendering components like the draw interaction mentioned above as child components of the react map component. The
map
instance could then be passed down to the children of the map component either directly as aprop
or by leveraging reactscontext
object using theProvider
pattern.
However, the react documentation explicitly advises against usingcontext
, though we found a number of solutions using this pattern (react-geo, react-leaflet) and also popular libraries likereact-redux
make use of it.
We therefore thought about usingReact.Children.map()
to clone the child components and then addingmap
to them as aprop
.
I hope the problem we are facing got clear enough. We do not know what would be the better way in terms of react
best practices.
What architecture would fit better to the "react way" of designing/managing and application and why?