【问题标题】:Add NavLink in Google maps infowindow (React + React Router v4)在 Google 地图信息窗口中添加 NavLink (React + React Router v4)
【发布时间】:2017-12-09 22:36:18
【问题描述】:

我正在使用带有 React 的 Google Maps,React Router v4(没有任何像 react-google-maps 这样的包)。我需要将 React Router v4 组件(NavLink 或 Link)放在 infowindow 中。我尝试使用 ReactDomServer.renderToString()。

const infoWindowContent = ReactDOMServer.renderToString(
            <NavLink to="project/5">Show</NavLink>
        );

this.infoWindow.setContent(infoWindowContent);

我的解决方案来自:Render react-router <Link> inside google InfoWindow

不幸的是它不起作用:

你不应该在 之外使用 or withRouter()

这很好理解,因为路由器超出范围。

你有什么解决方法的想法吗?

【问题讨论】:

    标签: reactjs google-maps-api-3 react-router-v4


    【解决方案1】:

    最后我切换到https://github.com/istarkov/google-map-react,这解决了我的问题。


    另一个想法(肮脏的解决方案):

    将历史记录放在全局窗口变量中:

    import createHistory from 'history/createBrowserHistory';
    
    if (!window.AppHistory) {
        window.AppHistory = createHistory();
    }
    

    在 infowindow 组件中这样做:

    const infoWindowContent = `<a class="infowindow" onclick="AppHistory.push('/project/${id}')">Show</a>`;
    
    this.infoWindow.setContent(infoWindowContent);
    

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2019-06-11
      相关资源
      最近更新 更多