无法在最新版本的create-react-app

时间:2020-07-29 12:17:46

标签: reactjs create-react-app

我正在使用 create-react-app 学习React,并且在我的应用中遇到此错误:

TypeError: react__WEBPACK_IMPORTED_MODULE_0___default.a.createRef() is not a function

现在,我知道此功能仅在React 16.3中引入,但这似乎是我已安装的版本。我通过键入以下内容来初始化所有内容:

npx create-react-app probando02

如果我检查我的版本:

npm view react version
16.13.1

我的package.json文件是:

{
  "name": "probando02",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "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"
    ]
  }
}

编辑:我的React环境,如npx create-react-app --info所示:

Environment Info:

  current version of create-react-app: 3.4.1
  running from C:\Users\paulo\AppData\Roaming\npm-cache\_npx\21716\node_modules\create-react-app

  System:
    OS: Windows 10 10.0.18362
    CPU: (24) x64 AMD Ryzen Threadripper 1920X 12-Core Processor
  Binaries:
    Node: 12.16.3 - C:\Program Files\nodejs\node.EXE
    Yarn: Not Found
    npm: 6.14.7 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: 44.18362.449.0
    Internet Explorer: 11.0.18362.1
  npmPackages:
    react: ^16.13.1 => 16.13.1
    react-dom: ^16.13.1 => 16.13.1
    react-scripts: 3.4.1 => 3.4.1
  npmGlobalPackages:
    create-react-app: Not Found

那是怎么回事?

1 个答案:

答案 0 :(得分:0)

我使用的是相同版本,但对我来说效果很好。

enter image description here 这也是我的package.json文件

{
  "name": "example",
  "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",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "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"
    ]
  }
}

在端口3000中启动默认网站对我来说效果很好。 我认为这只是一个更新问题,只需尝试更新npm。