I would like to resolve Invalid Hook Call Warning.
I am trying to create a portfolio using the api mode and react of rails.We use docker to build our environment and develop the back end and front end separately.
As a practice, I'm studying using teaching materials, but I got stuck because I got several errors below.
#1st
react.development.js:207Warning: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 have mismatching versions of React and the render (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You 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.
# second
react.development.js:1628 Uncaught TypeError: Cannot read properties of null (reading 'useRef')
useRef(react.development.js:1628:1)
at BrowserRouter (index.tsx:151:1)
at renderWithHooks (react-dom.development.js:16175:1)
at mountIndeterminateComponent(react-dom.development.js:20913:1)
at beginWork(react-dom.development.js:22416:1)
at HTMLUnknownElement.callCallback(react-dom.development.js:4161:1)
at Object.invokeGuardedCallbackDev(react-dom.development.js:4210:1)
at invokeGuardedCallback(react-dom.development.js:4274:1)
at beginWork $1 (react-dom.development.js:27405:1)
at performUnitOfWork(react-dom.development.js:26513:1)
# third
react-dom.development.js: 18572 The above error occurred in the <BrowserRouter>component:
at BrowserRouter (http://localhost:3000/static/js/bundle.js:1198:5)
at App
Consumer adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
App.tsx (some comments are being made)
import React from 'react';
import'./App.css';
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
// components
import {Restaurants} from './containers/Restaurants';
import {Foods} from './containers/Foods';
import {Orders} from './containers/Orders';
functionApp(){
return(
// <BrowserRouter>
// <Routes>
// // List of stores page
// <Route path="/restaurants">
// <Restaurants/>
// </Route>
// // Food List Page
// <Route path="/foods">
// <Foods/>
// </Route>
// // Order Page
// <Route path="/orders">
// <Orders/>
// </Route>
// </Routes>
// </BrowserRouter>
<h2>a</h2>
);
}
export default App;
Foods.tsx (remaining Restaurants, Orders are omitted because they are only characters)
export const Foods=()=>{
return(
<>
Food List
</>
)
}
https://ja.reactjs.org/warnings/invalid-hook-call-warning.html
I checked the error and found it in the official document, so I quoted it.
There are three possible causes:
The official website says:
You may be using react-dom (<16.8.0) or react-native (<0.60) that does not yet support hooks.You can run npmls react-dom or npmls react-native in the application folder to see which version you are using.If more than one appears, that can also be a problem (described later).
This time, the react-dom I am using is 18.1.0, so I thought this might be the cause, so I tried to downgrade the react-dom.
However, it will be as follows.It's probably a dependency issue.
I put it on hold here for now.
senseiy@senseIY-wsl:~/Practice/Rea-pra/playground $docker-compose exec front npm install [email protected]
npm ERR!code ERESOLVE
npm ERR!ERESOLVE could not resolve
US>npm ERR!
npm ERR! While resolving: undefined@undefined
npm ERR!Found: [email protected]
npm ERR!node_modules/react-dom
npm ERR!peer react-dom@">=16.8" from [email protected]
npm ERR!node_modules/react-router-dom
npm ERR!react-router-dom@"^6.3.0" from the root project
npm ERR!react-dom@"16.7.0" from the root project
US>npm ERR!
npm ERR! Could not resolve dependency:
npm ERR!react-dom@"16.7.0" from the root project
US>npm ERR!
npm ERR!Conflicting peer dependency:[email protected]
npm ERR!node_modules/react
npm ERR!peer react@"^16.0.0" from [email protected]
npm ERR!node_modules/react-dom
npm ERR!react-dom@"16.7.0" from the root project
US>npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR!this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
US>npm ERR!
npm ERR!See/root/.npm/eresolve-report.txt for a full report.
US>npm ERR!A complete log of this run can be found in:
npm ERR!/root/.npm/_logs/2022-05-18T06_14_24_182Z-debug-0.log
Regarding the violation of the hook rules, I thought it probably doesn't apply because I don't use a hook such as useState at this time.
The official website says:
For the hook to work, the react resolved when using the react import statement in your application code must be the same as the react imported in the react-dom package.
This warning occurs when these react import statements are resolved to separate objects.This can happen if you unintentionally include two copies of the react package in your project.
If you are using Node for package management, you can verify it by running the following in the project folder:
npmls react
If there are more than one copy of React, the dependency tree needs to be corrected to determine the cause.For example, your library may be using react as a dependency by mistake, not peer dependency.Yarn resolutions can be a workaround for the problem until the library is modified.
Therefore, we did the following, but looking at the results, it seems that there is no duplication.
senseiy@senseIY-wsl:~/Practice/Rea-pra/playground $docker-compose exec front npmls react
app@/usr/src/app
`-- [email protected]
+-- [email protected]
| `[email protected] deaduped
+-- [email protected]
| `[email protected] deaduped
`-- [email protected]
If you remove the App.tsx comment out listed above, you will get an error, but if you do not use the h2 tag or Router, the error will not occur and the characters will appear on the screen successfully.Therefore, there is probably a problem per BrowserRouter.But I don't know what to do from here.
The environment is
·React 18.1.0
·Rails 6.1.6
·Ruby 3.1.2
·We built the environment with Docker.
Simple Todo app created with Rails API x React x TypeScript
I followed this article.
If you have any advice, please let me know.
I edited part of App.tsx.I will write only the corrections.
<Routes>
// Store List Page
<Route path="/restaurants" element={<Restaurants/>}/>
// Food List Page
<Route path="/foods" element={<Foods/>}/>
// Order Page
<Route path="/orders" element={<Orders/>}/>
</Routes>
Currently, I checked with CodeSandbox (although only React TypeScript does not have rails) and it worked fine.The version is also
react
18.1.0
react-dom
18.1.0
react-router-dom
6.3.0
react-scripts
5.0.1
It is the same as the one in the Docker environment where the error is shown in .
Therefore, we believe that the version mismatch and misstatement on the React side are not the cause of the error.
If possible,
·Is the directory configuration wrong in the first place?
What bothered me was that there were two package.json files and two mode_modules each.My directory structure is
/Playground-/backend
|
-/frontend-/node_modules
| |
-d-c.yml (omitted) / -react-app-/node_modules
| |
| | - package.json
|
- Dockerfile, package.json, package-lock.json
It looks like this.Until now, I thought it was a specification, but is this directory configuration strange?Also, the contents of each package.json,node_modules are all different.
·In the first place, libraries such as react-router-dom have not been successfully installed
This may be the cause, but I didn't get an error, so I think it's probably not
·Is it because there is a Node outside the Docker environment?
We only build a Node environment on the PC itself.I remember practicing using the textbook with create-react-app only once.However, it should have nothing to do with the Docker environment, so it might be different.
I'm sorry, but I'm going to do multi-postingI'm sorry if I made you feel uncomfortable.
https://teratail.com/questions/jywvvw4u9bai2y
reactjs
It's solved.It seems that the react-router-dom installation method was bad.It will be my guess from here, so it may be wrong.
My name is
docker-compose run front yarn create react-app react-app --template typescript
I made a create react-app using yarn as shown in .However, I accidentally used npm when adding react-router-dom.
docker-compose exec front npm install react-router-dom
↓Correction
docker-compose exec front yarn add react-router-dom
* Now it works well.
Apparently, if you create-react-app with yarn, you will get an unexpected error if you don't add it with yarn add.
I hope I can be of service to someone.Thank you for your cooperation.
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
581 PHP ssh2_scp_send fails to send files as intended
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.