Invariant failed: You should not use outside a

后端 未结 20 1259
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 20:55

I use react-router-dom for routing in my React application. Part of my app extracted in another package. List of dependencies looks like this:

相关标签:
20条回答
  • 2020-12-03 21:05

    I solved this cryptic error by deleting the dependency of 'react-router-dom' from MyComponent. I deleted 'node_modules/react-router-dom' and from 'package.json' My App is structured as follows:

        AppFolder
        ├── README.md
        ├── build
        ├── node_modules
        │   ├── react-router-dom
        ├── package-lock.json
        ├── package.json
        ├── public
        │   ├── favicon.ico
        │   ├── index.html
        │   └── manifest.json
        └── src
            ├── App.css
            ├── App.js
            ├── App.test.js
            ├── index.js
            ├── redux
            │   ├── reducers.js
            │   └── store.js
            └── serviceWorker.js
    
    
        MyComponent
        ├── README.md
        ├── dist
        ├── node_modules
        │   ├── react-router-dom
        ├── package-lock.json
        ├── package.json
        ├── rollup.config.js
        └── src
            ├── index.js
            ├── styles.css
            └── test.js
    

    This is the source code for App.js

    import MyComponent from 'MyComponent'
    
    export default App(){
     return (
     <div className="App">
        <HashRouter>
          <div>
            <header className="App-header">
    
            </header>
            <Route path="/home" component={MyComponent}/>
          </div>
        </HashRouter>
      </div>)
    }
    

    This is the export for MyComponent

    export default withRouter(MyComponent);
    

    If 'react-router-dom' is left in the component folder, then the error appears.

    0 讨论(0)
  • 2020-12-03 21:08

    In my case, this error was caused due to mixing up usage of things (withRouter, MemoryRouter) from react-router and react-router-dom. By using only things from react-router-dom, the invariant error vanished. Hope this helps anybody.

    0 讨论(0)
  • 2020-12-03 21:09

    I managed to solve the problem by removing <BrowserRouter> from router.js and including it in app.js

    export default function  Routes() {
      return (
         
          <Switch>
            <Route path="/" exact component={Home} />
            <Route path="/book" component={Book} />
            <Route path="/client" component={Client} />
            <Route path="/rent" component={Rent} />
            <Route path="/dash" component={Dash} />
          </Switch>
        
      );
    }
    function App() {
      return (
        <div>
      
      <BrowserRouter>
        <Header></Header>
        
        <Routes/>
        </BrowserRouter>
        </div>
      );
    }
    
    0 讨论(0)
  • 2020-12-03 21:11

    I had this problem whilst testing and solved it by wrapping my test component with Routers.

    import React from 'react';
    import ReactDom from 'react-dom';
    import Header from '../components/Header/Header';
    import { BrowserRouter } from 'react-router-dom';
    
    it('renders Header without crashing', () => {
      const div = document.createElement('div');
    
      ReactDom.render(
        <BrowserRouter>
          <Header />
        </BrowserRouter>, 
      div);
    
      ReactDom.unmountComponentAtNode(div);
    });
    

    Jest, Enzyme: Invariant Violation: You should not use or , To test a component (with Jest) that contains and withRouter you need to import Router in you test, not in your component import { BrowserRouter as Invariant Violation: You should not use or withRouter() outside a According to react router 4 docs, the components are considered valid, where I can create components composed of s, then import them into another component and place inside a .

    https://xspdf.com/resolution/50584641.html

    0 讨论(0)
  • 2020-12-03 21:13

    You should include Route tag, Link tag inside the Router tag.

    import React, { Component } from 'react';
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    
    const Home = () => {
      return (
        <div>
          <p>Home</p>
        </div>
      );
    };
    
    const About = () => {
      return (
        <div>
          <p>About</p>
        </div>
      );
    };
    
    const Contact = () => {
      return (
        <div>
          <p>Contact</p>
        </div>
      );
    };
    class App extends Component {
      render() {
        return (
            <Router>
                  <div>
                  <h1>W3Adda - Simple SPA</h1>
                    <nav>
                      <ul>
                        <li>
                          <Link to="/">Home</Link>
                        </li>
                        <li>
                          <Link to="/about">About</Link>
                        </li>
                        <li>
                          <Link to="/contact">Users</Link>
                        </li>
                      </ul>
                    </nav>
    
                    <Route path="/" exact component={Home} />
                    <Route path="/about" component={About} />
                    <Route path="/contact" component={Contact} />
                  </div>
            </Router>
        );
      }
    }
    
    export default App;
    
    0 讨论(0)
  • 2020-12-03 21:15

    I had a similar problem with Redirect component. It turned out that I called Redirect from 'react-router' instead of 'react-router-dom'.

    Error: Invariant failed: You should not use <Redirect> outside a <Router>
    
    0 讨论(0)
提交回复
热议问题