2
我試圖實現開發人員使用react創建新頁面的能力,同時保持兼容性,以與我們用於所有頁面的主頁面模板(包括過濾器,選項卡等)一起工作具有2個入口點的React路由器
我需要是
我的當前解決方案包括被渲染成另一種來呈現導航組件到1個格的能力,與頁面內容(應用程序)(下面示出)分別呈現2個部件,但我在掙扎的地方是如何橋接路由器 他們倆。我遇到了路由器有2個實例的問題(我會從代碼中理解爲什麼),但我不確定如何構造這個實現我想要的行爲。
我如何能夠將這些組件渲染爲2個獨立的根div,但是仍然共享歷史記錄和導航路徑更改會反映到應用程序組件中?
import React } from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Link, Route, Switch } from 'react-router-dom';
import ContactPage from './ContactPage';
const HomePage =() => <div> Home page </div>;
const AboutPage =() => <div> This is an About Page </div>;
const Navigation =() => (
<div>
{/* Match structure of legacy tab structure, we then just replace current with a react component for navigation */}
<li className="dropdown" id="tab-menu-li">
<ul className="nav nav-tabs" id="tab-menu-list">
<li className="text-left">
<Link to={`/home`}>home</Link>
</li>
<li className="text-left">
<Link to={`/about`}>Contact Us</Link>
</li>
</ul>
</li>
{/* Renders into tab-menu-li fine if I put the switch in here
<Switch>
<Route path={`/home`} exact component={HomePage} />
<Route path={`/about`} exact component={ContactPage} />
</Switch>
*/}
</div>
);
const App =() => (
<Switch>
{/* How can I make this App component, catch the route changes from navigation?
This component draws fine, and /contact etc will work on page load. But any route changes triggered in navigation are never reflected here
*/}
<Route path={`/home`} exact component={HomePage} />
<Route path={`/about`} exact component={AboutPage} />
</Switch>
);
render(<BrowserRouter><Navigation /></BrowserRouter>, document.getElementById('tab-menu-li'));
render(<BrowserRouter><BaseLayout /></BrowserRouter>, document.getElementById('dataContainer'));
太棒了,這就是我所追求的魅力,謝謝! –