简体   繁体   English

设置反应路由器

[英]Setting up react router

I am trying to set up routing for a simple one page application. 我正在尝试为简单的单页应用程序设置路由。 When I say one page, I mean that all my components will be rendered at once. 当我说一页时,我的意思是我的所有组件都会立即渲染。 My aim with routing was to use it for link to anchor (clicking on the nav will scroll you to the correct section/component). 我对路由的目的是使用它来链接到锚点(点击导航将滚动到正确的部分/组件)。

index.js

No compiling errors but nothing gets rendered and I'm getting the following in my browser console. 没有编译错误,但没有任何东西得到渲染,我在浏览器控制台中得到以下内容。

浏览器控制台

App.js已更新

routes.js

I've reverted my App.js to its initial form when it was only rendering the components and working fine. 当它只渲染组件并且工作正常时,我已经将我的App.js恢复到它的初始形式。 Instead, I've created a separate file, routes.js to hold my routing. 相反,我创建了一个单独的文件routes.js来保存我的路由。 If this is correct, how can I get both rendered by my index.js. 如果这是正确的,我怎么能得到我的index.js渲染。 Or is this not the way things should work? 或者这不是事情的方式吗?

Router is not the default export of react-router . Router不是react-router的默认导出。 This is the correct require: 这是正确的要求:

const Router = require('react-router').Router;

You're also not supposed to return a Router from a render() method. 您也不应该从render()方法返回Router A <Router> must be passed directly to ReactDOM.render() . 必须将<Router>直接传递给ReactDOM.render()

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

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