10

I've been battling this one for a while!

I want to have the 'main app container' which always has the logo, navigation... I want to use react-bootstrap to pretty it up.

At the moment I'm running into problems, my project is based off davezuko's "react-redux-starter-kit"

I tried putting all of my bootstrap <NavBar> and <LinkContainers> in the Root container inside the provider.

Everything shows up and looks nice but none of my links work, and when I put a regular react-router <Link> I would run into the same problems.

I figured, well, links work in the views which is called by routes, so I copied all of this into the routes after export default (store) => (

Babel, eslint and webpack allow this to compile, but when I run the page none of this shows up, and when I take a look the react-dev console, these react nodes don't even appear.

Here's what I have, Root.js:

import React, { PropTypes } from 'react';
import { Provider } from 'react-redux';
import { Router } from 'react-router';

import { IndexLink, Link } from 'react-router';

import NavBar from 'react-bootstrap/lib/Navbar';
import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';

export default class Root extends React.Component {
  static propTypes = {
    history: PropTypes.object.isRequired,
    routes: PropTypes.element.isRequired,
    store: PropTypes.object.isRequired
  };

  get content () {
    return (
      <Router history={this.props.history}>
        {this.props.routes}
      </Router>
    );
  }

  get devTools () {
    if (__DEBUG__) {
      if (__DEBUG_NEW_WINDOW__) {
        if (!window.devToolsExtension) {
          require('../redux/utils/createDevToolsWindow').default(this.props.store);
        } else {
          window.devToolsExtension.open();
        }
      } else if (!window.devToolsExtension) {
        const DevTools = require('containers/DevTools').default;
        return <DevTools />;
      }
    }
  }

  render () {
    const styles = require('./../containers/Root.scss');
    return (
      <Provider store={this.props.store}>
        <div>
          <div className={styles.Root}>
            <Link to='login'>login</Link>
            <NavBar fixedTop>
              <NavBar.Header>
                <NavBar.Brand>
                  <IndexLink to='/' activeStyle={{color: '#33e0ff'}}>
                    <div className={styles.brand}></div>
                    <span>Hero Energy Solutions</span>
                  </IndexLink>
                </NavBar.Brand>
                <NavBar.Toggle />
              </NavBar.Header>
              <NavBar.Collapse eventKey={0}>
                <Nav navbar>
                  <LinkContainer to='/chat'>
                    <NavItem eventKey={1}>Chat</NavItem>
                  </LinkContainer>
                  <LinkContainer to='/widgets'>
                    <NavItem eventKey={2}>Widgets</NavItem>
                  </LinkContainer>
                  <LinkContainer to='/survey'>
                    <NavItem eventKey={3}>Survey</NavItem>
                  </LinkContainer>
                  <LinkContainer to='/about'>
                    <NavItem eventKey={4}>About Us</NavItem>
                  </LinkContainer>
                  <LinkContainer to='/'>
                    <NavItem eventKey={5}>Login</NavItem>
                  </LinkContainer>
                </Nav>
              </NavBar.Collapse>
            </NavBar>
          </div>
          {this.content}
          {this.devTools}
        </div>
      </Provider>
    );
  }
}

Routes.js:

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import CoreLayout from 'layouts/CoreLayout/CoreLayout';
import HomeView from 'views/HomeView/HomeView';
import LoginView from 'views/LoginView/LoginView';
import NotFoundView from 'views/NotFoundView/NotFoundView';
import RestrictedView from 'views/RestrictedView/RestrictedView';
import AboutView from 'views/AboutView/AboutView';

import { IndexLink, Link } from 'react-router';

import NavBar from 'react-bootstrap/lib/Navbar';
import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';

import {UserAuthWrapper} from 'redux-auth-wrapper';
import {routerActions} from 'react-router-redux';

const CheckAuth = UserAuthWrapper({
  authSelector: (state) => state.user, // how to get the user state
  redirectAction: routerActions.replace, // the redux action to dispatch for redirect
  wrapperDisplayName: 'CheckAuth', // a nice name for the auth check
  failureRedirectPath: 'login'  // default anyway but meh!
});
export default (store) => (
    <div>
      <Route path='/' component={CoreLayout}>
        <IndexRoute component={HomeView} />
        <Route path='login' component={LoginView} />
        <Route path='home' component={HomeView} />
        <Route path='about' component={AboutView} />
        <Route path='restricted' component={CheckAuth(RestrictedView)} />
      </Route>
      <Route path='*' component={NotFoundView}/>
    </div>
);

I'm not sure if this is of much help but here's a screen shot of the client side with the react console. Screenshot of react dev console:

Screenshot of react dev console

Mr Lister
  • 42,557
  • 14
  • 95
  • 136

1 Answers1

5

Sorry everyone! The solution is extremely simple.

The CoreLayout View, is where all of this stuff is supposed to go. My biggest problem was not properly understanding how react-router works! Now that I understand, here's the reasoning:

The Route / matches all requests that have a / in it (which is basically all requests). But it is a React Component, that contains other React Components! So the CoreLayout component is returned, but the content of CoreLayout is the corresponding View, i.e. About, Home...

Edit: You need to include <div> {this.props.children} </div> In the CoreLayout View where you want your other sub-views to be rendered (otherwise they won't render!).

lambda
  • 3,075
  • 1
  • 24
  • 31