I'm starting a new react app. So far, nothing weird has been added. This is how my package.json looks like:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"bootstrap": "^4.4.1",
"fibers": "^5.0.0",
"jquery": "^3.5.0",
"moment": "^2.24.0",
"node-sass": "^4.14.0",
"popper": "^1.0.1",
"react": "^16.13.1",
"react-bootstrap": "^1.0.1",
"react-dom": "^16.13.1",
"react-moment": "^0.9.7",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"react-thunk": "^1.0.0",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"sass": "^1.26.5",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Then I try the most simple of react hooks call within a component:
import React, { useState } from 'react';
const Gallery = () => {
const [test, setTest] = useState(null);
return <div>Gallery</div>;
};
export default Gallery;
But when I try to run it, I get:
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/warnings/invalid-hook-call-warning.html for tips about how to debug and
fix this problem.
The package.json seems to be ok. No duplicate react versions. React is 16.8+. And the component is a functional component, not a class-based one. What could I be missing?
NOTE: When I try to run the "npm ls react" suggested in the documentation, I get no versions listed. react-dom appears correctly. Could this be the cause? And if so, any ideas of how to fix it?
Problem, can be using different version of react & react-dom dependencies
If you are using react-router-dom and have Routes like this
return (
<Router>
<Switch>
<Route path="/" component={Home} exact />
<Route path="/gallery" render={Gallery} exact />
</Switch>
</Router>
);
Here, instead of <Route path="/gallery" render={Gallery} exact /> use this <Route path="/gallery" component={Gallery} />
That, solved my problem
Related
I try to deploy my typescript react app to Github pages, but every time when I access my URL, Github complains '404 there isn't a github pages site here.'
And I try to make my code simple, I just left the home page in my app, but it still complains.
If I don't use React router, it works. Why does it complain when I use react router?
My Code:
import Home from "./pages/Home" //rafce
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
const Router: React.FC = () => {
return <>
<BrowserRouter>
<Routes>
<Route path="/" ><Home /></Route>
</Routes>
</BrowserRouter>
</>
}
export default Router
My JSON:
{
"homepage": "http://lsk21007.github.io/ohno",
"name": "typescript-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^6.1.1",
"#fortawesome/free-regular-svg-icons": "^6.1.1",
"#fortawesome/free-solid-svg-icons": "^6.1.1",
"#fortawesome/react-fontawesome": "^0.1.18",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.5.1",
"#types/node": "^16.11.36",
"#types/react": "^18.0.9",
"#types/react-dom": "^18.0.4",
"bootstrap": "^5.2.0-beta1",
"react": "^18.1.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.1.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"typescript": "^4.6.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/styled-components": "^5.1.25",
"gh-pages": "^4.0.0"
}
}
Thanks for your help!!
I am using react with typescript in my project and also I am doing snapshot testing but when I run npm test command in vscode terminal I am getting error which I have attached in the attachement.
could you please let me know what should I do so that all the test case run successfully or any other thing I need to configure
Here is my package.json
{
"name": "vcc-collaboration-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fluentui/react": "^8.42.1",
"#fluentui/react-file-type-icons": "^8.5.6",
"#testing-library/jest-dom": "^5.15.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"#types/react-test-renderer": "^17.0.1",
"ag-grid-community": "^26.2.0",
"ag-grid-react": "^26.2.0",
"axios": "^0.24.0",
"babel-jest": "^26.6.0",
"jest": "^26.6.0",
"jest-watch-typeahead": "^1.0.0",
"moment": "^2.29.1",
"node-sass": "^6.0.1",
"react": "^17.0.2",
"react-bootstrap": "^2.0.2",
"react-dom": "^17.0.2",
"react-dropzone": "^11.4.2",
"react-grid": "^4.0.4",
"react-icons": "^4.3.1",
"react-router-dom": "^5.0.0",
"react-scripts": "4.0.3",
"react-test-renderer": "^17.0.2",
"vcc-ui": "^2.11.0",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/jest": "^27.0.3",
"#types/node": "^16.11.7",
"#types/react": "^17.0.35",
"#types/react-dom": "^17.0.11",
"#types/react-dropzone": "^5.1.0",
"#types/react-router": "^5.1.17",
"#types/react-router-dom": "^5.3.2",
"typescript": "^4.5.2"
}
}
EngineeringArea.test.tsx
import React from "react";
import * as ShallowRenderer from "react-test-renderer/shallow";
import EngineeringArea from "./EngineeringArea";
describe("EngineeringArea Screen", () => {
it("EngineeringArea Screen renders correctly", () => {
const renderer = ShallowRenderer.createRenderer();
const component = renderer.render(<EngineeringArea />);
expect(component).toMatchSnapshot();
});
});
You need to install specifically v0.6.5 of jest-watch-typeahead
npm i -D --exact jest-watch-typeahead#0.6.5
Had this error come up. My answer was using nvm to switch from node's current release (my system default) to its LTS (16.14.0) with nvm use 16
Also got this error message when trying to test. Changing node version worked for me. Here is link to the article:
https://github.com/facebook/create-react-app/issues/11792
Last answer was wrong. Here's the correct answer. https://github.com/facebook/create-react-app/issues/11043#issuecomment-942472592
In my case with Vite-React and with Create React App, works:
You need to install specifically v0.6.5 of jest-watch-typeahead
npm i -D --exact jest-watch-typeahead#0.6.5
Thank you Matt
I faced this issue while I was trying to create a new React Application using CRA with npx.
I followed what Matt suggested in his answer but jest started throwing an error saying, it's better to remove any babel-jest dependency, which is weird but I guess jest-watch-typehead` uses babel maybe (it's a wild guess, I have not checked).
Anyway, here's what I did based on the suggestion I got on console from the error thrown by babel-jest.
Removed package-lock.json file.
Removed node_modules/.
Did a fresh npm i.
I ran my tests again and got an error for not importing React in App.test.js, so I simply imported and it worked.
I have been using REACT for a while now and I can properly use hooks and am not breaking any hook rules here. for some reason, I performed npm install to reinstall all packages and after that, I'm getting this error.
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.
I also did reset to the previous commit to discard all changes but the error still remains.
this is my package.json file
{
"name": "client",
"proxy": "http://127.0.0.1:5000",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.11.3",
"#material-ui/lab": "^4.0.0-alpha.57",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"axios": "^0.21.1",
"bootstrap": "^4.6.0",
"mdbreact": "^5.0.1",
"react": "^17.0.1",
"react-bootstrap": "^1.5.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"uninstall-all-modules": "^1.0.5",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "npm run watch:css && react-scripts start",
"build": "npm run build:css && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:css": "postcss src/assets/tailwind.css -o src/assets/main.css",
"watch:css": "postcss src/assets/tailwind.css -o src/assets/main.css"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"autoprefixer": "^10.2.4",
"postcss": "^8.2.6",
"postcss-cli": "^8.3.1",
"tailwindcss": "^2.0.3"
}
}
All packages are up to date and we're working on this particular commit
None of the other answers to similar errors seem to fix my issue and the worst thing is I am not even able to figure out where the root of the error is. If other info is required pls mention it in the comments.
This issue doesn't always have to be related to the usage of hooks or any other react errors.
This time it was occurring because of a module that I was using "mdbreact". For some reason, their website is down and if your project uses it, you'll end up getting this error.
I created a new React App and npm start was working in localhost. After I closed it and went back later npm start will not open in the browser. I reinstalled everything and the exact same thing happened again. I tried installing react-scripts globally. Nope. Same thing. I deleted node_modules and still nothing. I tried removing babel from my package.json. Nothing. I cleared my cache from the browser. Nothing. Any help would be great. I have made many React Apps and never had anything like this.
Here is my package.json:
{
"name": "johnny_auth1_app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"bootstrap": "^4.5.3",
"react": "^17.0.1",
"react-bootstrap": "^1.4.0",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.1",
"styled-components": "^5.2.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Here is my App.js:
import './App.css';
import Home from './Components/Home';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import { NavigationBar } from './Components/NavigationBar';
const App = () => {
return (
<Router>
<NavigationBar />
<div classname='App'>
<Switch>
<Route exact path='/' component={Home}/>
</Switch>
</div>
</Router>
);
};```
This issue may be from the browser you are using. Copy the local host address and past it on different browsers. You may also need to restart your PC if you recently did some installations on the PC.
I am using react-firebaseui for authentication, according to the documentation i have written the code
import React from "react";
import StyledFirebaseAuth from "react-firebaseui/StyledFirebaseAuth";
import firebase from "firebase";
const firebaseConfig = {
...
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Configure FirebaseUI.
const uiConfig = {
// Popup signin flow rather than redirect flow.
signInFlow: "popup",
// Redirect to /signedIn after sign in is successful. Alternatively you can provide a callbacks.signInSuccess function.
// signInSuccessUrl: '/signedIn',
// We will display Google and Facebook as auth providers.
signInOptions: [firebase.auth.GoogleAuthProvider.PROVIDER_ID],
};
export const SignInWith = () => (
<>
<p>Login</p>
<StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={firebase.auth()} />
</>
);
but i keep getting this error
./node_modules/firebaseui/dist/esm.js
Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase').`.
I tried importing firebase using import firebase from 'firebase/app, but still the same error.
I tried commenting StyledFirebaseAuth import and tag and it is working, so I am sure the problem is with react-firebaseui.
This is my package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.11.0",
"#material-ui/icons": "^4.9.1",
"#reduxjs/toolkit": "^1.4.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"#types/jest": "^24.9.1",
"#types/node": "^12.19.2",
"#types/react": "^16.9.54",
"#types/react-dom": "^16.9.9",
"#types/react-redux": "^7.1.9",
"axios": "^0.21.0",
"firebase": "^8.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-firebaseui": "^4.1.0",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.0",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/react-router-dom": "^5.1.6",
"babel-plugin-import": "^1.13.1",
"customize-cra": "^1.0.0",
"react-app-rewired": "^2.1.6"
}
}
Thank you for any help.
Unfortunately, you've upgraded your "firebase" dependency to 8.0.0 but the "firebaseui" dependency doesn't support it yet. You will have to temporarily downgrade firebase to version 7.24.0 until firebaseui and react-firebaseui support the breaking changes in 8.0.0.
import firebase has changed with the version of 9.0.0, now there is compatibility available no need to downgrade, so you can use /compat folder in your imports,
import firebase from 'firebase/compat/app'