简体   繁体   中英

react-router: How come including <IndexRoute component={Audience}> stops my entry point APP.js from rendering at all?

When I include the IndexRoute line, my APP.js component doesn't render at all. When I remove it, the page renders fine. The renders are both done with React.render, so I'm not sure what's going on because I thought that had nothing to do with react-router. I had also commented out all the lines involving routes in the APP.js file. In my app-client.js file:

var React = require('react');

var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;

var APP = require('./components/APP');
var Audience = require('./components/Audience');
var Speaker = require('./components/Speaker');
var Board = require('./components/Board');

var routes = (
    <Router>
        <Route path="/" component={APP}>
            <IndexRoute component={Audience} />
            <Route path="speaker" component={Speaker} />
            <Route path="board" component={Board} />
        </Route>
    </Router>
);

//With Routes
//render(<Router>{routes}</Router>, document.getElementById('react-container'))

//Without Routes
React.render(<APP />, document.getElementById('react-container'));

I've been following the tutorial "Building a Polling App with Socket IO and React.js" on Lynda.com that uses react-router v0.13. Everything works fine so far such as the sockets between the client and server and rendering APP.js. I'm stuck at this point though. The APP.js file:

var React = require('react');
// var ReactRouter = require('react-router');
// var Router = ReactRouter.Router;

var io = require('socket.io-client');
var Header = require('./parts/Header');

var APP = React.createClass({

    getInitialState() {
        return {
            status: 'disconnected',
            title: '',
        }
    },

    componentWillMount() {
        this.socket = io('http://localhost:3000');
        this.socket.on('connect', this.connect);
        this.socket.on('disconnect', this.disconnect);
        this.socket.on('welcome', this.welcome);
    },

    connect() {
        this.setState({ status: 'connected' });
    },

    disconnect() {
        this.setState({ status: 'disconnected' });
    },

    welcome(serverState) {
        this.setState({ title: serverState.title });
    },

    render() {
        return (
            <div>
                <Header title={this.state.title} status={this.state.status} />
                // {this.props.children}
            </div>
        );
    }
});

module.exports = APP;

I think instead of :

render(<Router>{routes}</Router>, document.getElementById('react-container'))

You should render :

render({ routes }, document.getElementById('react-container'))

Because your router is already in the routes variable

I believe you also need to define 'IndexRoute'. Something like:

var IndexRoute = ReactRouter.IndexRoute;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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