React-router not displaying component

后端 未结 2 746
执笔经年
执笔经年 2021-02-15 15:07

I\'m currently learning react-router, and then trying to implement it in a sample app.

Here is my code:

index.html

         


        
2条回答
  •  抹茶落季
    2021-02-15 15:46

    The ugly solution is to get rid of nested routes like so:

    module.exports = (
        
            
            
        
    );
    

    Yet, I figured it out the right way to do via this doc

    We need another component (which I called Main) that will contain the right component to display (i.e. this.props.children).

    I use IndexRoute to define the default component to display.

    Here is the corrected code:

    /src/components/main.jsx (new)

    var React = require('react');
    
    module.exports = React.createClass({
        render: function() {
            return 
    // this is where you can add header {this.props.children} // this is where you can add footer
    } });

    /src/routes.jsx (modified)

    var React = require('react');
    var Router = require('react-router').Router;
    var Route = require('react-router').Route;
    var IndexRoute = require('react-router').IndexRoute;
    
    var Main = require('./components/main');
    var HelloWorld = require('./components/hello-world');
    var About = require('./components/about');
    
    module.exports = (
        
            
                
                
            
        
    );
    

提交回复
热议问题