Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating React and OpenLayers within a Redux Architecture

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:

  1. 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 of react-redux. While this solution provides an easy access to map we were wondering whether this would be a tractable approach since the store should be kept minimal and the map object never changes throughout the lifecycle of the application.

  2. 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 a prop or by leveraging reacts context object using the Provider pattern.
    However, the react documentation explicitly advises against using context, though we found a number of solutions using this pattern (react-geo, react-leaflet) and also popular libraries like react-redux make use of it.
    We therefore thought about using React.Children.map() to clone the child components and then adding map to them as a prop.

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?

like image 492
TobsenB Avatar asked Jan 26 '18 08:01

TobsenB


People also ask

How does Redux React to integrate?

Here, you can access fetchData as a prop in your react listing component, which dispatches an action to make an API call. mapDispatchToProps() is used to dispatch an action to store. In react-redux, components cannot access the store directly. The only way is to use connect().

Can Redux be used with React?

Redux itself is a standalone library that can be used with any UI layer or framework, including React, Angular, Vue, Ember, and vanilla JS.

What is the architecture of Redux?

Redux is an architecture in which all of your app's state lives in one container. The only way to change state is to create a new state based on the current state and a requested change. The Store holds all of your app's state. An Action is immutable data that describes a state change.

Can I use Usestate with Redux?

It is totally fine to use a mix of React component state and Redux state. You might for example use non-critical UI state inside React components, like if a checkbox is checked or not. The official Redux FAQ has a good list of rules of thumb for determining what kind of data should put into Redux.


1 Answers

I'm late to the party here, but six months ago I would have recommended using Context API as Redux was using it. As an alternate solution, I would have simply maintained a global object reference on window.app.cache.

Now, the React Context API is the way to go for this. Hope Saga didn't complicate the project!

like image 124
Nick Avatar answered Sep 28 '22 09:09

Nick