Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  109] [ 4]  / answers: 1 / hits: 15377  / 7 Years ago, sun, may 21, 2017, 12:00:00

I have a repo at https://bitbucket.org/codyc54321/anthony-project-react


I have the homepage showing links, and when you click the links react-router works. But the text of the component is not showing up, like <div>Login page</div>, <div>Homepage</div>, etc.


index.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'));

Nav.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;

components/Home.js:


import React, { Component } from 'react';

export default class Home extends Component {
render() {
return (
<div>Homepage</div>
);
}
}

Why aren't these components showing up on the page?


More From » reactjs

 Answers
156

Since your other routes are nested inside the route for 'Nav' component, it should render its children somewhere. For that, you have to put this.props.children where you want to render nested components in JSX of Nav component.



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>
)
}
}

[#57716] Thursday, May 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dahlias

Total Points: 730
Total Questions: 104
Total Answers: 101

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;