react-router 報錯The prop `history` is marked as required in `Router`, but its val

xiaobaiha發表於2018-07-24

在使用react-router(4.3.1)構建react路由時,進入首頁發現報錯

Warning: Failed prop type: The prop `history` is marked as required in `Router`, but its value is `undefined`.
    in Router (created by ReactRouter)
    in ReactRouter
複製程式碼

檢查程式碼

import React from 'react';
import {Route,Router,hashHistory} from 'react-router';
import App from '../App';
import Add from '../pages/Add';
import Modify from '../pages/Modify';
import Delete from '../pages/Delete';
import Query from '../pages/Query';

export default class ReactRouter extends React.Component{
  render(){
    return (
    <Router history={hashHistory}>
      <Route path='/'>
        <Route path='app' component={App}>
          <Route path='delete' component={Delete} />
          <Route path='add' component={Add} />
          <Route path='modify' component={Modify} />
          <Route path='query' component={Query} />
        </Route>
      </Route>
    </Router>)
  }
}
複製程式碼

發現已經新增了history這個屬性,那麼為什麼還會報這個錯誤呢 這是因為引入的react-router版本是4的,所以這種老式寫法不再支援,修改成V4新版本,使用react-router-dom庫後顯示正常。

相關文章