简体   繁体   English

react-router 没有渲染任何东西

[英]react-router is not rendering anything

I created my app with create-react-app and trying to use react-router on it.我使用create-react-app创建了我的应用程序,并尝试在其上使用react-router but it's not working at all.但它根本不起作用。

Here's my Header.js .这是我的Header.js

  import React, { Component } from 'react';

class Header extends Component {
    render() {
        return (
          <nav>
                <div className="nav-wrapper blue darken-1">
                    <a className="brand-logo center">Testing</a>

                    <div className="right">
                        <ul>
                            <li>
                                <a><i className="material-icons">vpn_key</i></a>
                            </li>
                            <li>
                                <a><i className="material-icons">lock_open</i></a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        );
    }
}

export default Header;

my App.js .我的App.js

import React, { Component } from 'react';
import Header from './Header';

class App extends Component {
  render() {
    return (
      <div className="App">
        <div className="Header">
        <Header />
        {this.props.children}
        </div>
      </div>
    );
  }
};

export default App;

and my index.js here.和我的index.js在这里。

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import App from './App';
import Home from './Home';
import Login from './Login';
import Register from './Register';

ReactDOM.render((
  <Router history={browserHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={Home}/>
            <Route path="/home" component={Home}/>
            <Route path="/login" component={Login}/>
            <Route path="/register" component={Register}/>
        </Route>
    </Router>),
  document.getElementById('root')
);
  • Don't worry about Home, Login, Register thing on index.js .不用担心index.js上的Home, Login, Register事情。 They are super fine.他们非常好。

Am I doing something wrong?难道我做错了什么?

Ho if you're v4* of react-router it won't work that way you should install react-router-dom and import it. 呵呵,如果您是react-router v4 *,它将无法正常工作,您应该安装react-router-dom并将其导入。

That's the way I did it 我就是那样做的

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


//modified code of yours
ReactDOM.render((
     <BrowserRouter>
       <div>
         <Route path="/" component={App}>
           <IndexRoute component={Home}/>
           <Route path="/home" component={Home}/>
           <Route path="/login" component={Login}/>
           <Route path="/register" component={Register}/>
         </Route>
      </div>
    </BrowserRouter>),
    document.getElementById('root'));

Note: Don't forget to wrap the route in a wrapper or it'll throw an error! 注意:不要忘记将路由包装在包装中,否则会引发错误!

and to set 404 page just provide another route without any path to it.If no routes found it will be rendered. 并设置404页面,只需提供另一条没有任何路径的路由。如果未找到任何路由,则将其呈现。

<Route component={FourOhFour} /> /* FourOhFour component as 404*/

** Bonus point If you're new to react-router you may fall in a problem of rendering multiple routes at the same time. **奖励点如果您是React Router的新手,则可能会遇到同时渲染多条路线的问题。 But you want to render only one route at a time. 但是您一次只想渲染一条路线。 at this point. 这一点。

/*Import Switch also*/
import {BrowserRouter, Route, Switch} from 'react-router-dom';

Then 然后

/*Wrap your route with `Switch` component*/
<BrowserRouter>
    <div>
        <Switch>
            <IndexRoute component={Home}/>
            <Route path="/" component={App}/>
            <Route path="/home" component={Home}/>
            <Route path="/login" component={Login}/>
            <Route path="/register" component={Register}/>
         </Switch>
    </div>
</BrowserRouter>

If you are using React Router 4, as said by other comments, you have to use 'react-router-dom' in order to import it in your component. 如果您使用的是React Router 4,如其他评论所述,则必须使用“ react-router-dom”才能将其导入组件中。

import { BrowserRouter as Router } from 'react-router-dom'

you can now give any component as a children of Router (it has to be a single node). 现在,您可以将任何组件作为Router的子代(必须是单个节点)。

The main difference from the normal RR2-3 is that now every route is a simple component, and you can put it along with other components. 与常规RR2-3的主要区别在于,现在每条路由都是一个简单的组件,您可以将其与其他组件一起使用。

You don't have IndexRoute anymore, you just put the routes in the order you want: 您不再具有IndexRoute,只需按所需的顺序放置路由即可:

<div>
  <Header />
  <Route path="/blabla/:bla" component={SingleBlaBla} />
  <Route path="/aboutUs" exact component={AboutUs} />
  <Route path="/" exact component={Home} />
  <Footer />
</div>

there are more than a couple of things to understand, in the options of Route, the use of Switch, Redirect and other very useful components. 在“路由”选项,“交换器”,“重定向”和其他非常有用的组件的选项中,需要了解的内容不只两件事。 Try to spend some time on some good documentation since it is a very good version, and it will stick around for a while. 因为它是一个非常好的版本,所以请花一些时间在一些好的文档上,并且它将持续一段时间。

If you can have a look at this wonderful introduction: https://egghead.io/courses/add-routing-to-react-apps-using-react-router-v4 如果您可以看一下这个精彩的介绍,请访问: https : //egghead.io/courses/add-routing-to-react-apps-using-react-router-v4

and the doc website: https://reacttraining.com/react-router/ 和doc网站: https//reacttraining.com/react-router/

The correct format is正确的格式是

import React from 'react';
import { BrowserRouter as Router, Routes, Route,  } from "react- 
router-dom";
import './App.css';
import Home from './components/Pages/Home';

function App() {
return (
 <>
 <Router>
    <Routes>
    <Route exact path='/' element={<Home/>}/>
    </Routes>
  </Router>
 </>
)} 
export default App;

I read all the answers here but didn't manage to solve my problem, untill I reached out to the 'react-router-dom' guide itself.我在这里阅读了所有答案,但没有设法解决我的问题,直到我联系了“react-router-dom”指南本身。 you can read the simplifies guide there and in about 5~10 mins you will figure this out.你可以在那里阅读简化指南,大约 5~10 分钟后你就会明白这一点。 mainly, it was wrong placing with the components.主要是组件放置错误。

https:\/\/github.com\/remix-run\/react-router\/blob\/main\/docs\/getting-started\/tutorial.md<\/a> https:\/\/github.com\/remix-run\/react-router\/blob\/main\/docs\/getting-started\/tutorial.md<\/a>

"

May not be your case but this could help someone else.可能不是您的情况,但这可以帮助其他人。 In my case none of it worked.在我的情况下,它都没有奏效。 Turned out to be a case with parcel cache.原来是包裹缓存的情况。 Delete the parcel cache folder and ran npm run dev again and it worked.删除包裹缓存文件夹并再次运行npm run dev并且它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM