React项目出现'ReactCurrentOwner'未定义TypeError报错求助
Hey there, let's work through that TypeError: Cannot read property 'ReactCurrentOwner' of undefined error you're seeing. After digging into your code, dependencies, and setup, here are the root issues and straightforward fixes:
1. React/ReactDOM Version Mismatch & Duplicate React Copies
This is the biggest culprit here:
- Your
package.jsonlistsreact-domas version2.2.2—that's extremely outdated and completely incompatible with yourreact@15.6.1installation. React and ReactDOM must always share the same major/minor version to work together properly. - You're also loading two separate instances of React: one from your local npm install, and another from the CDN scripts in your HTML. This creates a conflict where React's internal variables (like
ReactCurrentOwner) aren't accessible across the mismatched copies.
Fix Steps:
- Update
react-domin yourpackage.jsonto match React's version:"react-dom": "15.6.1" - Remove the two CDN script tags from your HTML file. Your webpack build will bundle React and ReactDOM into your app automatically, so these external copies are redundant and harmful.
2. Typo in Babel Preset Version
Your babel-preset-env has a typo in the version number (1.5,2 instead of 1.5.2). This can cause broken transpilation, which might lead to unexpected issues with React's internal code.
Fix:
Correct the version in package.json:
"babel-preset-env": "1.5.2"
3. Refresh Dependencies & Rebuild
After fixing the above, you need to clean up and rebuild to ensure everything is synced:
- Run
yarn installagain to pull the correct versions of ReactDOM andbabel-preset-env. - Delete your existing build output (usually
public/bundle.jsor similar) to get rid of any leftover old code. - Run
yarn buildto create a fresh bundle, thenyarn serveto start your app.
Quick Note on Component Code
While it's not causing your current error, I spotted a small outdated pattern in your handleAddOption method: you're using var len = 0;. Since you're using React classes and modern JS, switching to let (or const if the value doesn't change) would be more consistent, but this is optional for now.
Once you apply these fixes, the ReactCurrentOwner error should disappear, and your Indecision app should run like it did before adding the new feature.
内容的提问来源于stack exchange,提问作者Nishanth Reddy

