React Router only recognises index route

后端 未结 2 897
感情败类
感情败类 2021-01-07 04:19

I have 2 routes, / and /about and i\'ve tested with several more. All routes only render the home component which is /.

When

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 05:03

    Using ES6 and the newest react-router would look like this:

    import React from 'react';
    import {
      Router,
      Route,
      IndexRoute,
    }
    from 'react-router';
    
    const routes = (
      
        
          
        
      
    );
    
    const Home = React.createClass({
      render() {
        return (
          
    this is the main component {this.props.children}
    ); } }); //Remember to have your about component either imported or //defined somewhere React.render(routes, document.body);

    On a side note, if you want to match unfound route to a specific view, use this:

    
    

    Notice the path is set to *

    Also write your own NotFound component.

    Mine looks like this:

    const NotFound = React.createClass({
      render(){
       let _location = window.location.href;
        return(
          

    You have reached:

    {_location}

    ); } });

提交回复
热议问题