prop`history`在`Router`中被标记为required,但是它的值为`undefined`.在路由器 [英] The prop `history` is marked as required in `Router`, but its value is `undefined`. in Router

查看:33
本文介绍了prop`history`在`Router`中被标记为required,但是它的值为`undefined`.在路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ReactJs 的新手.这是我的代码:

I am new to ReactJs. This is my code:

var React = require('react');
var ReactDOM = require('react-dom');
var {Route, Router, IndexRoute, hashHistory} = require('react-router');
var Main = require('Main');
ReactDOM.render(
  <Router history={hashHistory}>
  <Route path="/" component={Main}></Route>
</Router>, document.getElementById('app'));

并用 webpack 编译它.我还向别名添加了 Main 组件.控制台抛出这些错误:我还阅读了这些链接:

and compiling it with webpack. Also I added Main component to my aliases. The console throws these errors: I also read these links :

反应路由器失败的道具历史",未定义

怎么做当值未定义时,我解析历史记录被标记为必需?

升级 React-Router 并用 browserHistory 替换 hashHistory

在网上进行了很多搜索,但我无法解决这个问题.React Router 是版本 4

and many searches around the web, but I couldn't fix this issue. React Router is version 4

推荐答案

如果您使用的是 react-router v4,则还需要安装 react-router-dom.之后,从 react-router-dom 导入 BrowserRouter 并为 BrowserRouter 切换 Router.似乎 v4 改变了几件事.此外,react-router 文档已经过时.这是我的工作代码:

If you are using react-router v4 you need to install react-router-dom as well. After that, import BrowserRouter from react-router-dom and switch Router for BrowserRouter. It seems that v4 change several things. Also, the react-router documentation is outdated. This is my working code:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route } from 'react-router-dom'
import App from './components/App';

ReactDOM.render((
     <BrowserRouter>
          <Route path="/" component={App}/>
     </BrowserRouter>
     ),
     document.getElementById('root')
);

来源

这篇关于prop`history`在`Router`中被标记为required,但是它的值为`undefined`.在路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆