Invariant Violation: You should not use outside a
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
add a comment |
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
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
add a comment |
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
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
javascript reactjs react-router
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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>
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
|
show 3 more comments
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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
|
show 3 more comments
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>
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
|
show 3 more comments
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>
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>
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
|
show 3 more comments
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
|
show 3 more comments
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 30 '18 at 13:43
Fábio Rodrigues FonsecaFábio Rodrigues Fonseca
414
414
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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