Versions:
- react-router-dom 4.1.1
- react-router-redux 5.0.0-alpha.4
- react-leaflet 1.1.3
- leaflet 1.0.3
Steps to reproduce
I create a leaflet map. In which I add some markers. These markers have popups.
In each of these popup I want to have a <Link>
Also if it helps this is my Routing config:
ReactDOM.render(
<Provider store={store}>
<div>
<AppContainer />
<ConnectedRouter history={history}>
<div>
<MenuContainer />
<Switch>
<Route path='/:area/:sport/list' component={ListContainer} />
<Route path='/:area/:sport/map' component={MapContainer} />
<Route path='/:area/:sport/rasp' component={RaspContainer} />
<Route path='/:shortcode/details' component={StationDetailsContainer} />
<Redirect exact from='/' to='/wellington/paragliding/list' />
<Route component={NoMatch} />
</Switch>
</div>
</ConnectedRouter>
</div>
</Provider>,
document.getElementById('root')
)
Expected Behavior
I can see my link and click on it when popup opens.
Actual Behavior
Impossible to see the link. It's not generated.
Extra details
Inside my <MapMode>
I use <Map>
from leaflet.
If I set a <Link>
just above the <Map>
tag it works.
As soon as I want to have a link inside my <Map>
, somehow it breaks.
This is the React structure of my page, <Popup>
tag just contains null
as Javascript is breaking:
It's quite a complex problem so feel free to ask me questions. Thanks.
I tried the solution suggested by Tharaka but it didn't work for me. It looks like react-leaflet's Popup is using it's own context, thus blocking context that is passed from higher levels. However, inspired by this solution I came up with another one, really simple & based on the composition principal.
I created RouterForwarder component
and then used it in my component (the one that renders Map, Marker, Popup & Link) in the following way:
I'm not 100% sure about this answer. But anyway I'm going to try because I think at least it might shed some light to anyone who will try to solve this problem in future.
I got the first hint from this issue in
react-leaflet
GitHub repo. According to that and your error, it seems the problem is Popup can't access therouter
from thecontext
becausecontext
isn't passed into the Popup with the way they render it. So we should be able to fix the problem if we can explicitly pass the context to Popup.Then I found a way to explicitly pass the context into a component in this StackOverflow answer. With that, I think you should be able to use a HoC(Higher order Component) as follows to solve your problem.
This is the HoC that inject context to a component:
Let's say you are using Popup inside a component called MapMaker. Then you can inject the context with
router
into Popup using the HoC like this.My solution (it's a workaround but works great and I see no downsides):
I solved by using (in react router v3 with redux)
whereas
goTo
is defined in