我尝试添加默认页面,用户打开网站时首先会转到该页面,但它无法呈现默认路由。我该如何解决此问题?react router v6注意:没有路由器也能正常工作,所以不应该是组件问题
import React, {useState} from 'react';
import './App.css';
import {BrowserRouter as Router, Routes, Route, Switch} from 'react-router-dom';
import AddNote from './components/noteDisplay/addNote/newNote.js'
import NoteDisplay from './components/noteDisplay/noteDisplay.js'
import secondPage from './components/secondPage/secondContainer.js'
const App = () => {
return (
<div className="App">
<h1 className="app-title">My notes</h1>
<Router>
<div>
<Routes>
<Route path='/second' component={secondPage}/>
<Route path='*' component={NoteDisplay}/>
</Routes>
</div>
</Router>
</div>
);
}
export default App;
3条答案
按热度按时间c9x0cxw01#
与v5相比,
react-router-dom
v6Route
组件API发生了重大变化。component
属性、render
和children
函数属性消失了,取而代之的是一个element
属性,该属性采用ReactElement
,又名JSX。路线和路线
从旧的
Route
component
prop切换到element
prop,并将路由的组件作为JSX而不是对该组件的引用传递。lb3vh1jj2#
用户首先看到的页面是主页,用“/"指定。要将其设为默认值,可以使用Route组件的index prop,如下所示
pbpqsu0x3#