React default create-react-app App component rendered twice - reactjs

I started new React project via the command:
npx create-react-app my-app --template typescript
Then, I changed the App component to look like that:
import * as React from 'react';
export default class App extends React.Component<{}> {
public render() {
console.log("App Rendered");
return (
<div>
Hello World
</div>
);
}
}
and when I watch the Console log ( both in Google Chrome and Microsoft Edge ) I receive this messages:
[HMR] Waiting for update signal from WDS...
App Rendered
App Rendered
My Questions:
Why the App render is being called twice?
How to remove the first message with that WDS?
This is my package.json ( default one as I ran create-react-app ):
{
"name": "my-app",
"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",
"#types/jest": "^24.0.0",
"#types/node": "^12.0.0",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "~3.7.2"
},
"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"
]
}
}

When wrapped with React.StrictMode (which is how CRA template are setup), on development, renders will be called twice. This is done to catch bugs that might occurs when some setState functions are not pure. You can read this for more detail.

Remove React.StrictMode from src/index.tsx

Related

React js application isn't running on localhost:3000

I have tried many many times but my http://localhost:3000 not working at all.
Just taking reload & reload
Console log is also clear. nothing at there.
1st time when I faced this problem, I cleared the history, then it works but now this is not working,
After giving so much pressure, it shows me,"
Aw, Snap!
something went wrong while displaying this webpage.
"
my app.js file:
import './App.css';
function App() {
return (
<div className="App">
<h1>Hello World</h1>
</div>
);
}
export default App;
my package.json file:
{
"name": "secret-name",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.1.1",
"#testing-library/user-event": "^13.5.0",
"bootstrap": "^5.1.3",
"react": "^18.1.0",
"react-bootstrap": "^2.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.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"
]
}
}
Just the title of my website appears at the top.
I can't create a new react app because this is an assignment and I have pushed the primary code to the private repository. I don't want more problems.
After facing this problem, I deleted the package.lock.json & node_modules and then again install the packages by npm install.
What can I do now?
I think that something went wrong when you create the project.
Try to create a new one
npx create-react-app my_app_name
And the run
npm run start

I am having a difficult time getting a simple React JS to start or run

I am having a difficult time getting a simple React JS to start or run. Her are my 2 simple files and package.json.
App.js
export default App;const App = () => {
return (
<div className="container">
<h1>Hello World</h1>
</div>
)
}
export default App
index.js
import { render } from ReactDOM
import App from './App'
render(<App />, document.getElementById('app'))
package.json
{
"name": "first-react-application",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.13.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"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"
]
}
}
I have tried start index.js and start App.js. I don't get any errors server just won't run. I try to connect to 127.0.0.0:3000 and localhost:3000 and my browser never connects.
Any help would be greatly appreciated.
Thanks
Dawson
Get rid of the ‘ export default App;’ before the const in App.js

Adding Chakra UI after starting development of Create React App

I'm getting an error after I tried adding ChakraUI to my create-react-app. Hoping for any help (also suggestions for what to add to a stackoverflow question is also appreciated)
I ran yarn add #chakra-ui/react #emotion/react#^11 #emotion/styled#^11 framer-motion#^4 as suggested in https://chakra-ui.com/docs/getting-started
I used CRA to start my app but decided to add chakra after I already started a bit of development. That's why I didn't use yarn create react-app my-app --template #chakra-ui
However after running yarn & yarn start I get this error about framer-motion:
TypeError: framer_motion__WEBPACK_IMPORTED_MODULE_1__.motion.custom is not a function
my index.js file looks like this:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { ChakraProvider } from '#chakra-ui/react';
ReactDOM.render(
<React.StrictMode>
<ChakraProvider>
<App />
</ChakraProvider>
</React.StrictMode>,
document.getElementById('root')
);
package.json:
{
"name": "gad-questionaire",
"version": "0.1.0",
"private": true,
"dependencies": {
"#chakra-ui/react": "^1.3.4",
"#emotion/react": "^11",
"#emotion/styled": "^11",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"framer-motion": "^4",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"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"
]
}
}
Using react version: 17.0.1
npm i framer-motion#3.10.6
👆 worked for me, 4.0 appears to have breaking changes

Npm start won't start React App and no 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.

Why remote library works but local library throws error?

I am building a component library, and install it as a local package, but it throws an error when I use react Hooks.
import React from "react";
import { useSelector } from "react-redux";
export default function Info() {
const name = useSelector((state) => state.account.name);
return (
<>
<ul>
<li>name: {name}</li>
</ul>
</>
);
}
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Then I upload this component library to remote, install it again, it solved.
But I still didn't know why...
Could anyone explain it?
My package.json
{
"name": "package",
"version": "0.1.0",
"private": true,
"main": "dist/index.js",
"babel": {
"presets": [
[
"react-app"
]
]
},
"dependencies": {},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"compile": "rm -rf dist && mkdir dist && cross-env NODE_ENV=development babel ./src/components -d dist --copy-files"
},
"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": {
"#babel/cli": "^7.12.0",
"cross-env": "^7.0.2",
"react-redux": "^7.2.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-scripts": "3.4.3",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2"
},
}
Is there any difference behavior between the local package and remote package?
I read many articles, I guess it might be the multiple instances of React.
But why remote package wouldn't have this problem?
Articles I read:
Hooks + multiple instances of React #13991
Invalid Hook Call Warning
remove the brackets
const name = useSelector(state => state.account.name);
Add curly braces, and remove name in the end.. as you are trying to destructure the state.
const { name } = useSelector((state) => state.account);

Resources