當嘗試路由到不同的螢幕時,我一直收到錯誤訊息「在位置「/home」匹配的葉子路由沒有元素或元件。這意味著它將預設渲染一個帶有空值的 <Outlet />
,導致頁面為空。」
import './App.css'; import { BrowserRouter as Router, Routes, Route } from "react-router-dom" import React from "react"; import Home from './Screens/Home'; import Sleep from './Screens/Sleep'; import Brew from './Screens/Brew'; import Navigation from './Navigation'; import Steam from './Screens/Steam'; function App() { return( <div className="App"> <Router> <Navigation/> <Routes> <Route exact path="/home" component={<Home />} /> <Route path="/sleep" component={<Sleep />} /> <Route path="/brew" component={<Brew />} /> <Route path="/steam" component={<Steam />} /> </Routes> </Router> </div> ) } export default App;
Navigation.js,用於在螢幕之間進行路由的底部選項卡導航
import React from 'react'; import { Nav, NavItem } from 'reactstrap' import { NavLink } from 'react-router-dom'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faSearch, faHome, faUserCircle } from '@fortawesome/free-solid-svg-icons'; const tabs = [{ route: "/home", icon: faHome, label: "Home" },{ route: "/sleep", icon: faSearch, label: "Sleep" },{ route: "/brew", icon: faUserCircle, label: "Brew" },{ route: "/steam", icon: faUserCircle, label: "Steam" }] const Navigation = (props) => { return ( <div> {/* Bottom Tab Navigator*/} <nav className="navbar fixed-bottom navbar-light" role="navigation"> <Nav className="w-100"> <div className=" d-flex flex-row justify-content-around w-100"> {tabs.map((tab, index) => ( <NavItem key={`tab-${index}`}> <NavLink to={tab.route} className="nav-link bottom-nav-link" activeClassName="active"> <div className="row d-flex flex-column justify-content-center align-items-center"> <FontAwesomeIcon size="lg" icon={tab.icon} /> <div className="bottom-tab-label">{tab.label}</div> </div> </NavLink> </NavItem> ))} </div> </Nav> </nav> </div> ) }; export default Navigation;
我嘗試了多種解決方案,但都沒有成功。我有一種感覺,這與我混合使用了多個程式碼片段並試圖將它們組合在一起有關。
react-router@6的Route元件使用一個element屬性,接受一個ReactNode(例如JSX)或Component屬性,接受一個React Element。
查看Route的element/Component。
或
換句話說,不要使用上面的第二個範例,因為它是一種非最佳化的方法。