我正在尝试使用哈希历史记录在浏览器中开始使用React Router,我从版本3的指南中复制了此代码。

import React from "react";
import ReactDOM from "react-dom";
import {  Router,  Route,  Link, hashHistory } from 'react-router';

const App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/about">About</Link></li>
          <li><Link to="/inbox">Inbox</Link></li>
        </ul>
        {this.props.children}
      </div>
    )
  }
})

const About = React.createClass({
  render() {
    return <h3>About</h3>
  }
})

const Inbox = React.createClass({
  render() {
    return (
      <div>
        <h2>Inbox</h2>
        {this.props.children || "Welcome to your Inbox"}
      </div>
    )
  }
})

const Message = React.createClass({
  render() {
    return <h3>Message {this.props.params.id}</h3>
  }
})

var history = hashHistory;

console.log(history);

ReactDOM.render((
  <Router history="{history}">
    <Route path="/" component={App}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox}>
        <Route path="messages/:id" component={Message} />
      </Route>
    </Route>
  </Router>
),
document.getElementById('app-root'));


当我打开文档时出现这些错误


  警告:道具类型失败:history类型的道具string无效
  提供给Router,预期为object


我在此之前登录history,它看起来像一个对象

javascript - React Router历史记录是字符串吗?-LMLPHP


  未捕获的错误:您提供了使用历史记录v4.x或v2.x及更早版本创建的历史记录对象。此版本的React Router仅
  与v3历史记录对象兼容。请更改为历史记录v3.x。


我已经安装了react-router@3history@3,我可以确认

+-react-router@3.0.2无关

我在列表中找不到history,但是如果转到目录,我会看到package.json

"version": "3.3.0"

最佳答案

从历史记录中删除“”符号

<Router history={history}>

10-01 07:26
查看更多