create-react-app: new react apps state is undefined on class based component without a constructor
Describe the bug
Using state on a class based component without a constructor and creating methods will not compile because they are undefined. Using the EXACT same code in a react app I created over the weekend, it compiles and runs fine. Trying to create a new react app via ‘npx create-react-app <projectname>’ will not compile.
Did you try recovering your dependencies?
Yes I tried removing node_modules and creating new react apps via ‘npx create-react-app test’ numerous times
Which terms did you search for in User Guide?
State undefined class based component
Environment
It was not copies to my clipboard directly so heres my copy paste: Environment Info:
current version of create-react-app: 4.0.3 running from /Users/joseph/.npm/_npx/c67e74de0542c87c/node_modules/create-react-app
System: OS: macOS 11.1 CPU: (8) x64 Intel® Core™ i7-7700HQ CPU @ 2.80GHz Binaries: Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node Yarn: Not Found npm: 7.5.6 - ~/.nvm/versions/node/v14.15.4/bin/npm Browsers: Chrome: 88.0.4324.192 Edge: Not Found Firefox: 85.0.2 Safari: 14.0.2 npmPackages: react: ^17.0.1 => 17.0.1 react-dom: ^17.0.1 => 17.0.1 react-scripts: 4.0.3 => 4.0.3 npmGlobalPackages: create-react-app: Not Found
Steps to reproduce
(Write your steps here:)
- npx create-react-app test
- create a class based component
- initialize state without the use of a constructor or write an arrow function.
Expected behavior
State would be initialized and my app would compile
Actual behavior
state is undefined. method is undefined. screenshot
Reproducible demo
I do not have the time at the moment to upload a link to a project. I will try to get a repo up soon if you need
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 22
- Comments: 16
We are having the same issue not only for class component without constructor it is happening even with constructor as well. Please kindly notify if there are any workaround.
Same
Issue got fixed now automatically without any code change. While doing
yarn build
andyarn start
it is working fine now. Thanks!Is this issue related to babel? Babel just updated from 7.12.x to 7.13.x. I got an issue that arrow function not working in react class. Fixed it with downgrading babel back to 7.12.x
check out @svgr/webpack > @babel/preset-env and @babel/preset-react