0

I have tried implementing react-router routing library to my react application... here is my main component code. When i have just the '<Route path="/" component={App} />' without '<Route path="/admin" component={Admin} />' The App component gets rendered but when I have both, nothing gets rendered. was wondering if i was doing something wrong.

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import injectTapEventPlugin from 'react-tap-event-plugin';
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom';

import LandingPage from './landingpage';
import Admin from './admin';

injectTapEventPlugin();

class App extends React.Component{
  render(){
    return (
      <MuiThemeProvider>
        <LandingPage />
      </MuiThemeProvider>
    );
  }
}

ReactDOM.render(
  <Router>
    <Route path="/" component={App} />
    <Route path="/admin" component={Admin} />
  </Router>,
  document.getElementById('spot')
);

heres my admin component

import React from 'react';

class Admin extends React.Component {
  render(){
    return (
      <div>Holla</div>
    );
  }
}

export default Admin;

and my webpack configuration

module.exports = {
  entry: [
    './src/app.js'
  ],
  output: {
    path: __dirname,
    filename: "bundle.js",
    publicPath: '/'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      test: /\.jsx?$/,
      loader: 'babel'
    }]
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './',
    hot: true,
  }
};
2
  • 1
    Have you tried adding Switch to render your routes exclusively? reacttraining.com/react-router/web/api/Switch You may also need to add the exact attribute to your home path, like this: <Router> <Route exact path="/" component={App} /> <Route path="/admin" component={Admin} /> </Router> Commented Aug 5, 2017 at 19:56
  • Thank you.. the exact was a more subtle answer Commented Aug 5, 2017 at 20:01

2 Answers 2

2

React-Router 4 accepts only one child. Try:

   <Router>
    <Switch>
      <Route path="/" component={App} />
      <Route path="/admin" component={Admin} />
    </Switch>
  </Router>,

or

   <Router>
    <div>
      <Route path="/" component={App} />
      <Route path="/admin" component={Admin} />
    </div>
  </Router>,
Sign up to request clarification or add additional context in comments.

1 Comment

don't forget to import switch from react-router-dom! but alex is right.
0

I think your problem is with the MUI theme provider not being available for your admin component and that the router only accepts one child. Try placing your muithemeprovider here instead:

ReactDOM.render(

  <Router>
  <MuiThemeProvider>
    <Route path="/" component={App} />
    <Route path="/admin" component={Admin} />
  </MuiThemeProvider>      
  </Router>,
  document.getElementById('spot')
);

So that it's available for all your components and not just App

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.