Invariant Violation: You should not use outside a










15















I have a problem that I dont know how to solve, I get this error when running npm test Invariant Violation: You should not use <Switch> outside a <Router>. What can the problem be and how can I solve it? The test I run is the standard app.test.js that comes with react



class App extends Component 
render()
return (
<div className = 'app'>
<nav>
<ul>
<li><Link exact activeClassName="current" to='/'>Home</Link></li>
<li><Link exact activeClassName="current" to='/TicTacToe'>TicTacToe</Link></li>
<li><Link exact activeClassName="current" to='/NumGame'>Quick Maths</Link></li>
<li><Link exact activeClassName="current" to='/HighScore'>Highscore</Link></li>
<li><Link exact activeClassName="current" to='/Profile'>Profile</Link></li>
<li><Link exact activeClassName="current" to='/Login'>Sign out</Link></li>
</ul>
</nav>
<Switch>
<Route exact path='/' component=Home></Route>
<Route path='/TicTacToe' component=TicTacToe></Route>
<Route path='/NumGame' component=NumberGame></Route>
<Route path='/HighScore' component=HighScore></Route>
<Route path='/Profile' component=Profile></Route>
<Route path='/Login' component=SignOut1></Route>
</Switch>
</div>
);

;









share|improve this question
























  • Note that this error doesn't have anything to do with the fact you're running a unit test - you'd get it at runtime too.

    – Joe Clay
    May 29 '18 at 13:00















15















I have a problem that I dont know how to solve, I get this error when running npm test Invariant Violation: You should not use <Switch> outside a <Router>. What can the problem be and how can I solve it? The test I run is the standard app.test.js that comes with react



class App extends Component 
render()
return (
<div className = 'app'>
<nav>
<ul>
<li><Link exact activeClassName="current" to='/'>Home</Link></li>
<li><Link exact activeClassName="current" to='/TicTacToe'>TicTacToe</Link></li>
<li><Link exact activeClassName="current" to='/NumGame'>Quick Maths</Link></li>
<li><Link exact activeClassName="current" to='/HighScore'>Highscore</Link></li>
<li><Link exact activeClassName="current" to='/Profile'>Profile</Link></li>
<li><Link exact activeClassName="current" to='/Login'>Sign out</Link></li>
</ul>
</nav>
<Switch>
<Route exact path='/' component=Home></Route>
<Route path='/TicTacToe' component=TicTacToe></Route>
<Route path='/NumGame' component=NumberGame></Route>
<Route path='/HighScore' component=HighScore></Route>
<Route path='/Profile' component=Profile></Route>
<Route path='/Login' component=SignOut1></Route>
</Switch>
</div>
);

;









share|improve this question
























  • Note that this error doesn't have anything to do with the fact you're running a unit test - you'd get it at runtime too.

    – Joe Clay
    May 29 '18 at 13:00













15












15








15


1






I have a problem that I dont know how to solve, I get this error when running npm test Invariant Violation: You should not use <Switch> outside a <Router>. What can the problem be and how can I solve it? The test I run is the standard app.test.js that comes with react



class App extends Component 
render()
return (
<div className = 'app'>
<nav>
<ul>
<li><Link exact activeClassName="current" to='/'>Home</Link></li>
<li><Link exact activeClassName="current" to='/TicTacToe'>TicTacToe</Link></li>
<li><Link exact activeClassName="current" to='/NumGame'>Quick Maths</Link></li>
<li><Link exact activeClassName="current" to='/HighScore'>Highscore</Link></li>
<li><Link exact activeClassName="current" to='/Profile'>Profile</Link></li>
<li><Link exact activeClassName="current" to='/Login'>Sign out</Link></li>
</ul>
</nav>
<Switch>
<Route exact path='/' component=Home></Route>
<Route path='/TicTacToe' component=TicTacToe></Route>
<Route path='/NumGame' component=NumberGame></Route>
<Route path='/HighScore' component=HighScore></Route>
<Route path='/Profile' component=Profile></Route>
<Route path='/Login' component=SignOut1></Route>
</Switch>
</div>
);

;









share|improve this question
















I have a problem that I dont know how to solve, I get this error when running npm test Invariant Violation: You should not use <Switch> outside a <Router>. What can the problem be and how can I solve it? The test I run is the standard app.test.js that comes with react



class App extends Component 
render()
return (
<div className = 'app'>
<nav>
<ul>
<li><Link exact activeClassName="current" to='/'>Home</Link></li>
<li><Link exact activeClassName="current" to='/TicTacToe'>TicTacToe</Link></li>
<li><Link exact activeClassName="current" to='/NumGame'>Quick Maths</Link></li>
<li><Link exact activeClassName="current" to='/HighScore'>Highscore</Link></li>
<li><Link exact activeClassName="current" to='/Profile'>Profile</Link></li>
<li><Link exact activeClassName="current" to='/Login'>Sign out</Link></li>
</ul>
</nav>
<Switch>
<Route exact path='/' component=Home></Route>
<Route path='/TicTacToe' component=TicTacToe></Route>
<Route path='/NumGame' component=NumberGame></Route>
<Route path='/HighScore' component=HighScore></Route>
<Route path='/Profile' component=Profile></Route>
<Route path='/Login' component=SignOut1></Route>
</Switch>
</div>
);

;






javascript reactjs react-router






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 29 '18 at 12:48









John Kugelman

247k54406460




247k54406460










asked May 29 '18 at 12:38









Fille_MFille_M

108116




108116












  • Note that this error doesn't have anything to do with the fact you're running a unit test - you'd get it at runtime too.

    – Joe Clay
    May 29 '18 at 13:00

















  • Note that this error doesn't have anything to do with the fact you're running a unit test - you'd get it at runtime too.

    – Joe Clay
    May 29 '18 at 13:00
















Note that this error doesn't have anything to do with the fact you're running a unit test - you'd get it at runtime too.

– Joe Clay
May 29 '18 at 13:00





Note that this error doesn't have anything to do with the fact you're running a unit test - you'd get it at runtime too.

– Joe Clay
May 29 '18 at 13:00












2 Answers
2






active

oldest

votes


















29














The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and its alternatives are the common low-level interface for all router components and they make use of the HTML 5 history API, and you need this to navigate back and forth between your routes.



Try doing this rather



import BrowserRouter, Switch, Route from 'react-router-dom';


And then wrap everything like this



<BrowserRouter>
<Switch>
//your routes here
</Switch>
</BrowserRouter>





share|improve this answer

























  • Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

    – Fille_M
    May 29 '18 at 13:08







  • 1





    @Fille_M Restart your development server

    – casraf
    May 29 '18 at 13:10












  • Yes, Try @casraf suggestion

    – brandNew
    May 29 '18 at 13:32







  • 1





    It works now, thank you everyone!

    – Fille_M
    May 29 '18 at 13:48






  • 1





    <BrowserRouter/> should be </BrowserRouter>

    – Andy Feng
    Mar 13 at 0:33


















1














Always put BrowserRouter in the navegations components, follow the example:



import React, Component from 'react'
import render from 'react-dom'
import BrowserRouter, Route, NavLink, Switch from 'react-router-dom'

var Componente1 = () => (<div>Componente 1</div>)
var Componente2 = () => (<div>Componente 2</div>)
var Componente3 = () => (<div>Componente 3</div>)

class Rotas extends Component
render()

return (
<Switch>
<Route exact path='/' component=Componente1></Route>
<Route exact path='/comp2' component=Componente2></Route>
<Route exact path='/comp3' component=Componente3></Route>
</Switch>
)




class Navegacao extends Component
render()
return (

<ul>
<li>
<NavLink to="/">Comp1</NavLink>
</li>
<li>
<NavLink exact to="/comp2">Comp2</NavLink>
</li>
<li>
<NavLink exact to="/comp3">Comp3</NavLink>
</li>
</ul>
)



class App extends Component
render()
return (
<BrowserRouter>
<div>
<Navegacao />
<Rotas />
</div>
</BrowserRouter>
)



render(<App/>, document.getElementById("root"))


Note: the BrowserRouter accept only one children element.






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%2f50584641%2finvariant-violation-you-should-not-use-switch-outside-a-router%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    29














    The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and its alternatives are the common low-level interface for all router components and they make use of the HTML 5 history API, and you need this to navigate back and forth between your routes.



    Try doing this rather



    import BrowserRouter, Switch, Route from 'react-router-dom';


    And then wrap everything like this



    <BrowserRouter>
    <Switch>
    //your routes here
    </Switch>
    </BrowserRouter>





    share|improve this answer

























    • Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

      – Fille_M
      May 29 '18 at 13:08







    • 1





      @Fille_M Restart your development server

      – casraf
      May 29 '18 at 13:10












    • Yes, Try @casraf suggestion

      – brandNew
      May 29 '18 at 13:32







    • 1





      It works now, thank you everyone!

      – Fille_M
      May 29 '18 at 13:48






    • 1





      <BrowserRouter/> should be </BrowserRouter>

      – Andy Feng
      Mar 13 at 0:33















    29














    The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and its alternatives are the common low-level interface for all router components and they make use of the HTML 5 history API, and you need this to navigate back and forth between your routes.



    Try doing this rather



    import BrowserRouter, Switch, Route from 'react-router-dom';


    And then wrap everything like this



    <BrowserRouter>
    <Switch>
    //your routes here
    </Switch>
    </BrowserRouter>





    share|improve this answer

























    • Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

      – Fille_M
      May 29 '18 at 13:08







    • 1





      @Fille_M Restart your development server

      – casraf
      May 29 '18 at 13:10












    • Yes, Try @casraf suggestion

      – brandNew
      May 29 '18 at 13:32







    • 1





      It works now, thank you everyone!

      – Fille_M
      May 29 '18 at 13:48






    • 1





      <BrowserRouter/> should be </BrowserRouter>

      – Andy Feng
      Mar 13 at 0:33













    29












    29








    29







    The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and its alternatives are the common low-level interface for all router components and they make use of the HTML 5 history API, and you need this to navigate back and forth between your routes.



    Try doing this rather



    import BrowserRouter, Switch, Route from 'react-router-dom';


    And then wrap everything like this



    <BrowserRouter>
    <Switch>
    //your routes here
    </Switch>
    </BrowserRouter>





    share|improve this answer















    The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and its alternatives are the common low-level interface for all router components and they make use of the HTML 5 history API, and you need this to navigate back and forth between your routes.



    Try doing this rather



    import BrowserRouter, Switch, Route from 'react-router-dom';


    And then wrap everything like this



    <BrowserRouter>
    <Switch>
    //your routes here
    </Switch>
    </BrowserRouter>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 13 at 7:10

























    answered May 29 '18 at 12:44









    brandNewbrandNew

    1,773625




    1,773625












    • Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

      – Fille_M
      May 29 '18 at 13:08







    • 1





      @Fille_M Restart your development server

      – casraf
      May 29 '18 at 13:10












    • Yes, Try @casraf suggestion

      – brandNew
      May 29 '18 at 13:32







    • 1





      It works now, thank you everyone!

      – Fille_M
      May 29 '18 at 13:48






    • 1





      <BrowserRouter/> should be </BrowserRouter>

      – Andy Feng
      Mar 13 at 0:33

















    • Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

      – Fille_M
      May 29 '18 at 13:08







    • 1





      @Fille_M Restart your development server

      – casraf
      May 29 '18 at 13:10












    • Yes, Try @casraf suggestion

      – brandNew
      May 29 '18 at 13:32







    • 1





      It works now, thank you everyone!

      – Fille_M
      May 29 '18 at 13:48






    • 1





      <BrowserRouter/> should be </BrowserRouter>

      – Andy Feng
      Mar 13 at 0:33
















    Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

    – Fille_M
    May 29 '18 at 13:08






    Thank you this solved that error! But now I get another one.. Error: Error watching file for changes: EMFILE at _errnoException (util.js:1019:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1360:9) npm ERR! Test failed. See above for more details.

    – Fille_M
    May 29 '18 at 13:08





    1




    1





    @Fille_M Restart your development server

    – casraf
    May 29 '18 at 13:10






    @Fille_M Restart your development server

    – casraf
    May 29 '18 at 13:10














    Yes, Try @casraf suggestion

    – brandNew
    May 29 '18 at 13:32






    Yes, Try @casraf suggestion

    – brandNew
    May 29 '18 at 13:32





    1




    1





    It works now, thank you everyone!

    – Fille_M
    May 29 '18 at 13:48





    It works now, thank you everyone!

    – Fille_M
    May 29 '18 at 13:48




    1




    1





    <BrowserRouter/> should be </BrowserRouter>

    – Andy Feng
    Mar 13 at 0:33





    <BrowserRouter/> should be </BrowserRouter>

    – Andy Feng
    Mar 13 at 0:33













    1














    Always put BrowserRouter in the navegations components, follow the example:



    import React, Component from 'react'
    import render from 'react-dom'
    import BrowserRouter, Route, NavLink, Switch from 'react-router-dom'

    var Componente1 = () => (<div>Componente 1</div>)
    var Componente2 = () => (<div>Componente 2</div>)
    var Componente3 = () => (<div>Componente 3</div>)

    class Rotas extends Component
    render()

    return (
    <Switch>
    <Route exact path='/' component=Componente1></Route>
    <Route exact path='/comp2' component=Componente2></Route>
    <Route exact path='/comp3' component=Componente3></Route>
    </Switch>
    )




    class Navegacao extends Component
    render()
    return (

    <ul>
    <li>
    <NavLink to="/">Comp1</NavLink>
    </li>
    <li>
    <NavLink exact to="/comp2">Comp2</NavLink>
    </li>
    <li>
    <NavLink exact to="/comp3">Comp3</NavLink>
    </li>
    </ul>
    )



    class App extends Component
    render()
    return (
    <BrowserRouter>
    <div>
    <Navegacao />
    <Rotas />
    </div>
    </BrowserRouter>
    )



    render(<App/>, document.getElementById("root"))


    Note: the BrowserRouter accept only one children element.






    share|improve this answer



























      1














      Always put BrowserRouter in the navegations components, follow the example:



      import React, Component from 'react'
      import render from 'react-dom'
      import BrowserRouter, Route, NavLink, Switch from 'react-router-dom'

      var Componente1 = () => (<div>Componente 1</div>)
      var Componente2 = () => (<div>Componente 2</div>)
      var Componente3 = () => (<div>Componente 3</div>)

      class Rotas extends Component
      render()

      return (
      <Switch>
      <Route exact path='/' component=Componente1></Route>
      <Route exact path='/comp2' component=Componente2></Route>
      <Route exact path='/comp3' component=Componente3></Route>
      </Switch>
      )




      class Navegacao extends Component
      render()
      return (

      <ul>
      <li>
      <NavLink to="/">Comp1</NavLink>
      </li>
      <li>
      <NavLink exact to="/comp2">Comp2</NavLink>
      </li>
      <li>
      <NavLink exact to="/comp3">Comp3</NavLink>
      </li>
      </ul>
      )



      class App extends Component
      render()
      return (
      <BrowserRouter>
      <div>
      <Navegacao />
      <Rotas />
      </div>
      </BrowserRouter>
      )



      render(<App/>, document.getElementById("root"))


      Note: the BrowserRouter accept only one children element.






      share|improve this answer

























        1












        1








        1







        Always put BrowserRouter in the navegations components, follow the example:



        import React, Component from 'react'
        import render from 'react-dom'
        import BrowserRouter, Route, NavLink, Switch from 'react-router-dom'

        var Componente1 = () => (<div>Componente 1</div>)
        var Componente2 = () => (<div>Componente 2</div>)
        var Componente3 = () => (<div>Componente 3</div>)

        class Rotas extends Component
        render()

        return (
        <Switch>
        <Route exact path='/' component=Componente1></Route>
        <Route exact path='/comp2' component=Componente2></Route>
        <Route exact path='/comp3' component=Componente3></Route>
        </Switch>
        )




        class Navegacao extends Component
        render()
        return (

        <ul>
        <li>
        <NavLink to="/">Comp1</NavLink>
        </li>
        <li>
        <NavLink exact to="/comp2">Comp2</NavLink>
        </li>
        <li>
        <NavLink exact to="/comp3">Comp3</NavLink>
        </li>
        </ul>
        )



        class App extends Component
        render()
        return (
        <BrowserRouter>
        <div>
        <Navegacao />
        <Rotas />
        </div>
        </BrowserRouter>
        )



        render(<App/>, document.getElementById("root"))


        Note: the BrowserRouter accept only one children element.






        share|improve this answer













        Always put BrowserRouter in the navegations components, follow the example:



        import React, Component from 'react'
        import render from 'react-dom'
        import BrowserRouter, Route, NavLink, Switch from 'react-router-dom'

        var Componente1 = () => (<div>Componente 1</div>)
        var Componente2 = () => (<div>Componente 2</div>)
        var Componente3 = () => (<div>Componente 3</div>)

        class Rotas extends Component
        render()

        return (
        <Switch>
        <Route exact path='/' component=Componente1></Route>
        <Route exact path='/comp2' component=Componente2></Route>
        <Route exact path='/comp3' component=Componente3></Route>
        </Switch>
        )




        class Navegacao extends Component
        render()
        return (

        <ul>
        <li>
        <NavLink to="/">Comp1</NavLink>
        </li>
        <li>
        <NavLink exact to="/comp2">Comp2</NavLink>
        </li>
        <li>
        <NavLink exact to="/comp3">Comp3</NavLink>
        </li>
        </ul>
        )



        class App extends Component
        render()
        return (
        <BrowserRouter>
        <div>
        <Navegacao />
        <Rotas />
        </div>
        </BrowserRouter>
        )



        render(<App/>, document.getElementById("root"))


        Note: the BrowserRouter accept only one children element.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 13:43









        Fábio Rodrigues FonsecaFábio Rodrigues Fonseca

        414




        414



























            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%2f50584641%2finvariant-violation-you-should-not-use-switch-outside-a-router%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20