Error: Invalid hook call. React, react-bootstrap



  • I am building a completely new CRA project (with typescript and without result 1)

    My actions:

    1. npx create-react-app my-app --template typescript

    2. npm install react-bootstrap bootstrap@5.1.3

    3. npm install --save @types/react-bootstrap

    4. changing component <App/>as follows: (I will add a picture. The code is not clear as to why, by marking it as a code, it is still like the text.) введите сюда описание изображения

      import React from 'react';
      // import Container from 'react-bootstrap/Container'
      // import Row from 'react-bootstrap/Row'
      // import Col from 'react-bootstrap/Col'
      

      import {Container, Row, Col} from 'react-bootstrap'

      const App = () => {
      return (
      <>
      <Container>
      <Row>
      <Col>1 of 2</Col>
      <Col>2 of 2</Col>
      </Row>
      <Row>
      <Col>1 of 3</Col>
      <Col>2 of 3</Col>
      <Col>3 of 3</Col>
      </Row>
      </Container>
      </>
      );
      }

      export {App}

    5. Starting the project

    6. I'm making a mistake:

    (If you remove Bootstrap components, everything works)

    Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

    1. You might have mismatching versions of React and the renderer (such as React DOM)
    2. You might be breaking the Rules of Hooks
    3. You might have more than one copy of React in the same app
      See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

    введите сюда описание изображения



  • Unfortunately, it wasn't possible to reproduce the mistake.

    1. npx create-react-app my-app --template typescript
    2. npm install react-bootstrap bootstrap@5.1.3
    3. npm install --save @types/react-bootstrap

    Dependency in package.json:

    "bootstrap": "^5.1.3",
    "react": "^17.0.2",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    

    Of those who have encountered and solved the problem, many refer to inconsistent versions of react and react-dom. Try this one: https://github.com/facebook/react/issues/13991#issuecomment-473246217 But in a global sense, it's more of a crutch. You have a reason somewhere else. Maybe there was another version of react or create-react-app globally?

    Try to settle dependence by yarn instead of npm.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2