简体   繁体   English

react-router:为什么包括 <IndexRoute component={Audience}> 完全阻止我的入口点APP.js渲染?

[英]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. 当我包含IndexRoute行时,我的APP.js组件根本不会呈现。 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. 渲染都使用React.render完成,所以我不确定发生了什么,因为我认为这与react-router无关。 I had also commented out all the lines involving routes in the APP.js file. 我还注释掉了APP.js文件中涉及路由的所有行。 In my app-client.js file: 在我的app-client.js文件中:

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. 我一直在Lynda.com上使用“ react-router v0.13”的教程“使用Socket IO和React.js构建轮询应用程序”。 Everything works fine so far such as the sockets between the client and server and rendering APP.js. 到目前为止,一切正常,例如客户端和服务器之间的套接字以及渲染APP.js。 I'm stuck at this point though. 不过,我仍然停留在这一点上。 The APP.js file: APP.js文件:

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 因为您的路由器已经在routes变量中

I believe you also need to define 'IndexRoute'. 我相信您还需要定义“ IndexRoute”。 Something like: 就像是:

var IndexRoute = ReactRouter.IndexRoute;

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

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