Invariant Violation: You should not use outside a

后端 未结 10 1400
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 00:30

I have a problem that I don\'t know how to solve, I get this error when running npm test

Invariant Violation: You should not use

相关标签:
10条回答
  • 2021-02-01 01:06

    i found a solution to resolve this issue

    here is the Example code

    import React from 'react';
    import Home from './HomeComponent/Home';
    import { Switch, Route } from 'react-router';
    import { BrowserRouter } from "react-router-dom";
    class App extends React.Component{
    render(){
    return(
             <BrowserRouter>
               <Switch>
                <Route path="/" render={props => (
                    <Home {...props}/>
                )}/>
              </Switch>
            </BrowserRouter>
        )
      }
    }
    export default App;
    
    0 讨论(0)
  • 2021-02-01 01:14

    You Should write your code like this

    import {BrowserRouter, Switch, Router} from 'react-router-dom
     
     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>
    
         <BrowserRouter>
            <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>
          <BrowserRouter/>
          </div>
        );
      }
    };
    
    0 讨论(0)
  • 2021-02-01 01:18

    The proper way to handle this, according to React Router devs, is to wrap your unit test in a Router. Using MemoryRouter is recommended in order to be able to reset the router between tests.

    You can still do something like the following:

    <BrowserRouter>
      <App />
    </BrowserRouter>
    

    Then in App:

    <Switch>
      <Route />
      <Route />
    </Switch>
    

    Your unit tests for App would normally be something like:

    const content = render(<App />); // Fails Unit test
    

    Update the unit test to:

    const content = render(<MemoryRouter><App /></MemoryRouter>); // Passes Unit test
    
    0 讨论(0)
  • 2021-02-01 01:20

    The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and 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>
    
    0 讨论(0)
提交回复
热议问题