why router is undefined in react js?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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









share|improve this question































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









    share|improve this question



























      0












      0








      0


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









      share|improve this question
















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






      javascript reactjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 18 '16 at 10:26









      Cherniv

      29.5k10105132




      29.5k10105132










      asked May 18 '16 at 10:23









      user944513user944513

      3,4781463128




      3,4781463128
























          4 Answers
          4






          active

          oldest

          votes


















          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.






          share|improve this answer































            0














            You may need to brace your returned JSX:



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





            share|improve this answer
























            • still giving the same error

              – user944513
              May 18 '16 at 10:29











            • do you have any idea ..how to remove this error

              – user944513
              May 18 '16 at 10:35



















            0














            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.






            share|improve this answer


























            • please see my setting file .I imported that ,,

              – user944513
              May 18 '16 at 10:27











            • please remove your error

              – user944513
              May 18 '16 at 10:35











            • but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

              – user944513
              May 18 '16 at 10:39











            • 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

              – fvgs
              May 18 '16 at 10:55











            • Please clarify above what error you're referring to.

              – fvgs
              May 18 '16 at 10:58



















            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






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37296784%2fwhy-router-is-undefined-in-react-js%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              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.






              share|improve this answer




























                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.






                share|improve this answer


























                  1












                  1








                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 14:14









                  Deepak LalwaniDeepak Lalwani

                  111




                  111

























                      0














                      You may need to brace your returned JSX:



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





                      share|improve this answer
























                      • still giving the same error

                        – user944513
                        May 18 '16 at 10:29











                      • do you have any idea ..how to remove this error

                        – user944513
                        May 18 '16 at 10:35
















                      0














                      You may need to brace your returned JSX:



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





                      share|improve this answer
























                      • still giving the same error

                        – user944513
                        May 18 '16 at 10:29











                      • do you have any idea ..how to remove this error

                        – user944513
                        May 18 '16 at 10:35














                      0












                      0








                      0







                      You may need to brace your returned JSX:



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





                      share|improve this answer













                      You may need to brace your returned JSX:



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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 18 '16 at 10:28









                      AndyAndy

                      30.5k73565




                      30.5k73565













                      • still giving the same error

                        – user944513
                        May 18 '16 at 10:29











                      • do you have any idea ..how to remove this error

                        – user944513
                        May 18 '16 at 10:35



















                      • still giving the same error

                        – user944513
                        May 18 '16 at 10:29











                      • do you have any idea ..how to remove this error

                        – user944513
                        May 18 '16 at 10:35

















                      still giving the same error

                      – user944513
                      May 18 '16 at 10:29





                      still giving the same error

                      – user944513
                      May 18 '16 at 10:29













                      do you have any idea ..how to remove this error

                      – user944513
                      May 18 '16 at 10:35





                      do you have any idea ..how to remove this error

                      – user944513
                      May 18 '16 at 10:35











                      0














                      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.






                      share|improve this answer


























                      • please see my setting file .I imported that ,,

                        – user944513
                        May 18 '16 at 10:27











                      • please remove your error

                        – user944513
                        May 18 '16 at 10:35











                      • but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

                        – user944513
                        May 18 '16 at 10:39











                      • 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

                        – fvgs
                        May 18 '16 at 10:55











                      • Please clarify above what error you're referring to.

                        – fvgs
                        May 18 '16 at 10:58
















                      0














                      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.






                      share|improve this answer


























                      • please see my setting file .I imported that ,,

                        – user944513
                        May 18 '16 at 10:27











                      • please remove your error

                        – user944513
                        May 18 '16 at 10:35











                      • but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

                        – user944513
                        May 18 '16 at 10:39











                      • 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

                        – fvgs
                        May 18 '16 at 10:55











                      • Please clarify above what error you're referring to.

                        – fvgs
                        May 18 '16 at 10:58














                      0












                      0








                      0







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 18 '16 at 10:58

























                      answered May 18 '16 at 10:26









                      fvgsfvgs

                      9,34572542




                      9,34572542













                      • please see my setting file .I imported that ,,

                        – user944513
                        May 18 '16 at 10:27











                      • please remove your error

                        – user944513
                        May 18 '16 at 10:35











                      • but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

                        – user944513
                        May 18 '16 at 10:39











                      • 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

                        – fvgs
                        May 18 '16 at 10:55











                      • Please clarify above what error you're referring to.

                        – fvgs
                        May 18 '16 at 10:58



















                      • please see my setting file .I imported that ,,

                        – user944513
                        May 18 '16 at 10:27











                      • please remove your error

                        – user944513
                        May 18 '16 at 10:35











                      • but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

                        – user944513
                        May 18 '16 at 10:39











                      • 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

                        – fvgs
                        May 18 '16 at 10:55











                      • Please clarify above what error you're referring to.

                        – fvgs
                        May 18 '16 at 10:58

















                      please see my setting file .I imported that ,,

                      – user944513
                      May 18 '16 at 10:27





                      please see my setting file .I imported that ,,

                      – user944513
                      May 18 '16 at 10:27













                      please remove your error

                      – user944513
                      May 18 '16 at 10:35





                      please remove your error

                      – user944513
                      May 18 '16 at 10:35













                      but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

                      – user944513
                      May 18 '16 at 10:39





                      but still it not display first component codepen.io/naveennsit/pen/KzLJqE?editors=1010

                      – user944513
                      May 18 '16 at 10:39













                      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

                      – fvgs
                      May 18 '16 at 10:55





                      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

                      – fvgs
                      May 18 '16 at 10:55













                      Please clarify above what error you're referring to.

                      – fvgs
                      May 18 '16 at 10:58





                      Please clarify above what error you're referring to.

                      – fvgs
                      May 18 '16 at 10:58











                      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






                      share|improve this answer




























                        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






                        share|improve this answer


























                          0












                          0








                          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






                          share|improve this answer













                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 18 '16 at 12:34









                          Nishanth MathaNishanth Matha

                          4,60211226




                          4,60211226






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37296784%2fwhy-router-is-undefined-in-react-js%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith