我在尝试处理React Routing时遇到问题。 我有一个项目在运行。我安装了react,react-dom,react-router。我导入了以下内容:
import { Router, Route, IndexRoute, hashHistory } from "react-router";
渲染方法看起来像这样:
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Featured}></IndexRoute>
<Route path="archives" component={Archives}></Route>
<Route path="settings" component={Settings}></Route>
</Route>
</Router>,
app
);
现在在我的应用程序中,我正在尝试创建一个类:
import React from 'react';
import { Link } from 'react-router';
export default class App extends React.Component {
navigate() {
console.log(this.props);
this.props.history.pushState(null, '/');
}
render() {
return (
<div>
{this.props.children}
<Link to="archives"><button>archives</button></Link>
<Link to="settings"><button>settings</button></Link>
<button onClick={this.navigate.bind(this)}>featured</button>
</div>
)
}
}
现在它抛出以下错误: 未捕获的TypeError:无法读取未定义的属性“pushState”
当我尝试console.log this.props时,它通常使用这些道具记录对象:location:Object,params:Object,route:Object,router:Object,routeParams:Object ...
我做了一项研究,但没有找到解决我问题的方法。尝试使用browserHistory,context.history,useHistory等,但没有修复它。
你能帮我找一个关于这个bug的解决方案吗? 这是package.json文件:
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server --content-base --inline --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-class-properties": "^6.18.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"history": "^1.17.0",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"react-router": "^3.0.0",
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"
}
}