1

When I have added react router it gives me follow errors enter image description here

here is main.js

var React = require('react');
var ReactDOM = require('react-dom');
var Router = require('react-router');
var Route = require('react-router').Route;
var Home = require("./components/homePage");
ReactDOM.render(
    <Router >
        <Route component={Home} path="/"></Route>
    </Router>
    , document.getElementById('app'));

Homepage.js

var React = require('react');
var Home = React.createClass({
    render: function(){
        return (
            <div >
                <h1>Home</h1>
            </div>
        );
    }
});

module.exports = Home;

There are many others questions related to same problem but none of them address the real issue or general reason or cause. other answers suggesting using react-router 4 alpha and i have tried beta4 but still same error

10
  • Possible duplicate of React.createElement: type is invalid -- expected a string, but got: object Commented Feb 3, 2017 at 15:40
  • according to mentioned question installed beta-4 but still not working Commented Feb 3, 2017 at 17:20
  • Are you working in Node.js? Commented Feb 3, 2017 at 17:22
  • yes node gulp browserify and other junk Commented Feb 3, 2017 at 17:22
  • What do you have in package.json file when in question is react-router? Commented Feb 3, 2017 at 17:23

2 Answers 2

3

You are importing React Router components wrongly, it should be:

var Router = require('react-router').Router

var Route = require('react-router').Route

Sign up to request clarification or add additional context in comments.

1 Comment

after trying import { Router, Route } from 'react-router' the app worked so i came to know your syntax was right. As the first time i have tried i changed only in one file and the error was triggering from multiple file but I was unable to find due to bundle.js giving no hint from which file error happened.
1

There was a similar question like yours, but tricky you cannot say it is a duplicate, at least the error is the same.

Based on the ES5 syntax you are using I guess your example is old. Try using the different version of the React Router.

Or just start with some new ES6 syntax React Router example.

1 Comment

according to mentioned question installed beta-4 but still not working. there is no syntax error

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.