尝试使用 React Router 时,我不断收到错误消息,指出我的 Route 必须在 Router 内,即使它已经在了。我已经尝试再次运行 npm install,并删除和读取 react router npm 包。错误信息是
Invariant failed: You should not use <Route> outside a <Router>
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter as Router } from 'react-router-dom';
ReactDOM.render(
<Router>
<React.StrictMode>
<App />
</React.StrictMode>
</Router>,
document.getElementById('root')
);
app.js
import logo from './logo.svg';
import './App.css';
import Main from './Components/Main/main';
import {BrowserRouter, Route,Link } from 'react-router-dom';
function App() {
return (
<div className="App">
<Route exact path='/'>
<Main />
</Route>
</div>
);
}
export default App;
main.js
export default function Main(props) {
const [anim,setAnim] = useState(false);
return (
<MainSection>
</MainSection>
)
}