1. 程式人生 > >react的報錯 You should not use ‘Link’ outside a ‘Router’

react的報錯 You should not use ‘Link’ outside a ‘Router’

環境:

  • “react”: “^16.4.0”,
  • “react-dom”: “^16.4.0”,
  • “react-redux”: “^5.0.7”,
  • “react-router”: “^4.3.1”,
  • “react-router-dom”: “^4.3.1”

我在Header元件中 寫了一個Link跳轉路由,一直報錯誤
You should not use <Link> outside a <Router>
產生這個原因是 Header的元件木有寫在BrowserRouter 裡導致

  • 解決辦法
class App extends Component {
	render() {
		return (
				<Provider store={store}>
					<BrowserRouter>
						<div>
							<Header />
							<Route path='/' exact component={Home}></Route>
							<Route path='/detail/:id' exact component={Detail}></Route>
							<Route path='/login' exact component={Login}></Route>
						</div>
					</BrowserRouter>
				</Provider>
			)
	}
}