gpt4 book ai didi

javascript - warning.js :36 Warning: Failed prop type: The prop `history` is marked as required in `Router` , 但其值为 `undefined`

转载 作者:行者123 更新时间:2023-11-28 17:53:24 24 4
gpt4 key购买 nike

根据以下代码获取标题中的错误:

import React from 'react'
// import { browserHistory, hashHistory, Router } from 'react-router'
// import createMemoryHistory from 'history/lib/createMemoryHistory'
import { browserHistory, hashHistory, Router, Route, Switch } from 'react-router-dom'
import Portfolio from './portfolio/Portfolio'
import Home from './home/Home'
import NoMatch from './NoMatch'

// const history = createMemoryHistory(location);
// console.log('history', history);

const Routes = () => {
return (
<Router history={browserHistory}>
<Route exact={ true } path="/" component={ Home }/>
<Route exact={ true } path="/portfolio" component={ Portfolio }/>
<Route component={ NoMatch } />
</Router>
);
}

export default Routes

enter image description here

最佳答案

从react-router-dom版本4.0开始,用BrowserRouter替换Router并使用Switch,Router不能有多个 child 。

import { browserHistory, hashHistory, BrowserRouter, Route, Switch } from 'react-router-dom';

并用以下代码替换路由:

const Routes = () => {
return (
<BrowserRouter history={browserHistory}>
<Switch>
<Route exact={ true } path="/" component={ Home }/>
<Route exact={ true } path="/portfolio"
component={ Portfolio }/>
<Route component={ NoMatch } />
</Switch>
</BrowserRouter>
);
}

关于javascript - warning.js :36 Warning: Failed prop type: The prop `history` is marked as required in `Router` , 但其值为 `undefined`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44972508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com