Error: Invalid hook call. React, react-bootstrap
-
I am building a completely new CRA project (with typescript and without result 1)
My actions:
npx create-react-app my-app --template typescript
npm install react-bootstrap bootstrap@5.1.3
npm install --save @types/react-bootstrap
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}
Starting the project
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:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- 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.
npx create-react-app my-app --template typescript
npm install react-bootstrap bootstrap@5.1.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.