2

Im working on a React proyect Ive cloned in a new pc from a repository of my github account.While trying to add Routes, I got the following error

 Failed to compile ./src/components/App/App.js Module not found: Can't resolve 'react-router-dom'.

I searched for a solution and found this post Failed to compile. Module not found: Can't resolve 'react-router-dom'.

So I installed react-router-dom as described in the answers of the post. But now, when I run 'npm run start', the following error shows up in the browser

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

Ive checked if I have more than one version of React, in case the problem is in fact the described in the third point, but 'npm ls react' only displays one version of React.

Im positive that Hooks worked in my proyect until I installed react-router-dom, so it cant be the second posibility.

So Im almost sure the problem is the one described in the first point. There is a conflict between the versions of React-Dom and React-Router-Dom. Here are the dependencies of the proyect:

"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.1",
"@types/react-router-dom": "^5.1.7",
"axios": "^0.21.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.0"
  },

Ive tried to reinstall React, but it doesnt work, I still have this issue. I hope I dont have to delete the proyect and clone it again. Please Help!! Thanks

Germán
  • 47
  • 5
  • Hi, can you please share the screenshot where you are using the React router? If you are using your router with useLocation or useHistory or similar. You must use it in the component-based function, not in the generic function. – Shailesh Jha Mar 11 '21 at 04:54

0 Answers0