0

could you please tell me why router is undefined in react js .I already import react-router.js file in my codepen

here is my code http://codepen.io/naveennsit/pen/pymqPa?editors=1010

class App extends React.Component {

    render() {
        return <Router>
          <Route path='/' component={first}></Route>
         <Route path='/about' component={second}></Route>
        </Router>
    }

}

class second extends React.Component {

    render() {
        return <label>second component</label>
    }

}
class first extends React.Component {

    render() {
        return <label>first component</label>
    }

}

React.render( < App / > , document.getElementById('app'))

4 Answers 4

1

Per the documentation, when you include the library via a <script> tag, the library will be available on window.ReactRouter.

You can add at the top of your file the following assignment:

{ Router } = ReactRouter;

This will expose the Router component from react-router to a variable Router, which is what your code expects.

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

4 Comments

please see my setting file .I imported that ,,
but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010
It may be because you are trying to use the deprecated React.render() method instead of ReactDOM. See the docs for more info: facebook.github.io/react/docs/tutorial.html
Please clarify above what error you're referring to.
1

You need to use BrowserRouter (react-router-dom) package instead of Router. Your code would look something like this :

<div>
<ReactRouter.Route path='/' component={first}></ReactRouter.Route>
<ReactRouter.Route path='/about' component={second}></ReactRouter.Route>
</div>

Also Notice div tag in ReactRouter otherwise you will get "A Router may have only one child element" error.

Comments

0

You may need to brace your returned JSX:

return (
  <Router>
    <Route path='/' component={first}></Route>
    <Route path='/about' component={second}></Route>
  </Router>
)

Comments

0

You should initialise the aliases when script tag is dropped.

Try something like:

var Router  = window.ReactRouter;
var Route  = window.ReactRouter.Ruote;

At the beginning of your code

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.