I’m new to React/Redux tools and still figuring out some idiomatic design patterns.
Here's a scenario:
A user adding a place marker (that has some associated metadata) to an interactive map (Leaflet), which should be held in state.
I’ve just come from MeteorJS, in that situation it would go something like:
- User defines marker location on map
- Leaflet callback with marker position
- Add marker properties to state object in MiniMongo
- A state observer see’s the state change
- Calls custom module to add marker to map, init some event handlers etc.
However; the subscribe hook in Redux doesn’t really give much to work with (compared to meteor - so far I’ve avoided it completely, and have been using onComponentDidMount/Update
for initialisation/responding to non-trivial state changes.
React's component lifecycle hooks work swimmingly when the ‘thing’ to be updated is a react component, but what about the above example - there is no React component to speak of?
So now I have:
- User defines marker location on map
- Leaflet callback with marker position
- Dispatch action
- Reducer updates state
- ?
Naive options
- Wrap redux subscribe with some kind of diff logic so I know what exactly needs to be updated in leaflet when state changes? This would not handle restore state gracefully though (see note below).
- Use renderless components, i.e. React components that don't render any UI (return null), but exist within the virtual DOM and are still plugged into the component life-cycle? Others seem to be using this pattern (eg1, eg2) - which would work nicely, but it smells.
Notes
- I'd like the component structured in a way such that the state (and map marker) can be restored without any explicit intervention (which my meteor example doesn't really cover) - e.g.
onComponentDidMount
approach does this (if app is initialised with existing state the UI automatically reflects this). - The map marker is just an example to illustrate the problem, I typically work on SPA's with many 3rd party components that fit this pattern so a general solution would be great.
Happy to provide more detail if required - thanks!