反应成分没有出现

时间:2017-05-21 01:24:57

标签: javascript reactjs react-router

我在https://bitbucket.org/codyc54321/anthony-project-react

有一个回购

我有主页显示链接,当你点击链接react-router工作。但该组件的文字未显示,例如<form> <div class="form container"> <!-- form elements --> </div> </form><div>Login page</div>等。

index.js:

<div>Homepage</div>

Nav.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, IndexRoute, browserHistory } from 'react-router';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';

import reducers from './reducers';

import Nav from './Nav';
import Home from './components/Home';
import Login from './components/Login';
import SignupMain from './components/SignupMain';


const createStoreWithMiddleware = applyMiddleware()(createStore);

ReactDOM.render(
    (
      <Provider store={createStoreWithMiddleware(reducers)}>
          <Router history={browserHistory}>
              <Route path="/" component={Nav}>
                  <IndexRoute component={Home}/>
                  <Route path='/login' component={Login}/>
                  <Route path='/signup-main' component={SignupMain}/>
              </Route>
          </Router>
      </Provider>
  ), document.querySelector('.container'));

部件/ Home.js:

import React from 'react';
import { Link } from 'react-router';

export default class Nav extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                <nav className="navbar navbar-default">
                    <div className="container-fluid">
                        <div className="navbar-header navbar-brand">
                            <a href="/">Home</a>
                        </div>
                    </div>
                    <div id="navbar" className="navbar-right">
                        <ul className="nav navbar-nav">
                            <li><Link activeClassName="active" to="/" onlyActiveOnIndex>Home</Link></li>
                            <li><Link activeClassName="active" to="/login" onlyActiveOnIndex>Login</Link></li>
                            <li><Link activeClassName="active" to="/signup-main" onlyActiveOnIndex>Signup</Link></li>
                        </ul>
                    </div>
                </nav>
            </div>
        )
    }
}


// export Nav as default;

为什么这些组件不显示在页面上?谢谢

1 个答案:

答案 0 :(得分:3)

由于您的其他路径嵌套在“Nav”组件的路径中,因此它应该将其子项呈现在某处。为此,您必须将this.props.children放在要在Nav组件的JSX中呈现嵌套组件的位置。

export default class Nav extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                <nav className="navbar navbar-default">
                    <div className="container-fluid">
                        <div className="navbar-header navbar-brand">
                            <a href="/">Home</a>
                        </div>
                    </div>
                    <div id="navbar" className="navbar-right">
                        <ul className="nav navbar-nav">
                            <li><Link activeClassName="active" to="/" onlyActiveOnIndex>Home</Link></li>
                            <li><Link activeClassName="active" to="/login" onlyActiveOnIndex>Login</Link></li>
                            <li><Link activeClassName="active" to="/signup-main" onlyActiveOnIndex>Signup</Link></li>
                        </ul>
                    </div>
                </nav>
               <div>{this.props.children}</div> 
            </div>
        )
    }
}