简体   繁体   English

React Router 3不会渲染

[英]React router 3 wont render

I am trying to go to a route that shows a sidebar and some content but I get undefined as part of my route and it just goes to a blank page. 我正尝试转到显示侧边栏和一些内容的路由,但是我undefined为路由的一部分,因此仅进入空白页面。 I am not quite sure why this is happening but any help would be appreciated. 我不太确定为什么会这样,但任何帮助将不胜感激。 I am pretty sure it has to do with there not being a route to get the courseId as a parameter, but I am not sure how to nest that in or how to get it to work. 我很确定这与没有将CourseId作为参数的途径有关,但是我不确定如何嵌套它或如何使其工作。

Here are my routes: 这是我的路线:

 import App from './components/App' import Course from './routes/Course/components/Course' import AnnouncementsSidebar from './routes/Course/routes/Announcements/components/Sidebar' import Announcements from './routes/Course/routes/Announcements/components/Announcements' import Announcement from './routes/Course/routes/Announcements/routes/Announcement/components/Announcement' import AssignmentsSidebar from './routes/Course/routes/Assignments/components/Sidebar' import Assignments from './routes/Course/routes/Assignments/components/Assignments' import Assignment from './routes/Course/routes/Assignments/routes/Assignment/components/Assignment' render( <Router history={browserHistory}> <Route path="/" component={App}> <Route path="course/:courseId" component={Course}> <Route path="announcements" components={{ sidebar: AnnouncementsSidebar, main: Announcements }}> <Route path=":announcementId" component={Announcement} /> </Route> <Route path="assignments" components={{ sidebar: AssignmentsSidebar, main: Assignments }}> <Route path=":assignmentId" component={Assignment} /> </Route> </Route> </Route> </Router>, document.getElementById('root') ) 

Here is the course component: 这是课程的组成部分:

 /*globals SideNavData:true */ const styles = {} styles.sidebar = { float: 'left', width: 200, padding: 20, borderRight: '1px solid #aaa', marginRight: 20 } class Course extends Component { render() { let { sidebar, main, children, params } = this.props let course = SideNavData[params.courseId] let content if (sidebar && main) { content = ( <div> <div className="Sidebar" style={styles.sidebar}> {sidebar} </div> <div className="Main" style={{ padding: 20 }}> {main} </div> </div> ) } else if (children) { content = children } else { content = <Dashboard /> } return ( <div> <h2>{course.name}</h2> <Nav course={course} /> {content} </div> ) } } module.exports = Course 

here is a codesandbox if needed: https://codesandbox.io/s/pw0xvj6r0m 如果需要,这里是一个codeandbox: https ://codesandbox.io/s/pw0xvj6r0m

You are reading courseId from router params but there isn't that parameter in Root route in Course component on line 19. 您正在从路由器参数中读取courseId ,但第19行的Course组件中的Root route中没有该参数。

let course = SideNavData[params.courseId];

That is why when clicking link in you Nav component it redirects you to /course/undefined/... 这就是为什么在导航组件中单击链接时会将您重定向到/ course / undefined / ...的原因。

EDIT: Actually, you never reference that parameter in your Router... So you will always have undefined course 编辑:实际上,您永远不会在路由器中引用该参数...所以您将始终具有未定义的过程

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

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