RefferenceError:找不到变量React

问题描述 投票:1回答:1

我不完全确定为什么我会收到此错误,因为node_modules中存在反应并在引用文件中导入

enter image description here

提到的App.js文件就是这个

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const unstated_1 = require("unstated");
const layouts_1 = __importDefault(require("./layouts"));
const App = () => (<unstated_1.Provider>
    <layouts_1.default />
  </unstated_1.Provider>);
exports.default = App;

这是从TypeScript ^输出的。非转换版本如下

import React from 'react'
import { Provider as StateProvider } from 'unstated'
import AppRoot from './layouts'

const App = () => (
  <StateProvider>
    <AppRoot />
  </StateProvider>
)

export default App

我的项目结构看起来像这样

packages/
  native-app/
    node_modules/
    ios
    index.js
    src/
      App.tsx
    dist/
      native-app/
        src/
          App.js
      server/
  server/

我觉得这可能与在dist文件夹中嵌套有关?我的主要反应原生index.js进口App这样的

import { AppRegistry } from 'react-native'
import App from './dist/native-app/src/App'

AppRegistry.registerComponent('MyApp', () => App)

注意:这看起来像monorepo,但我没有使用任何像纱线工作区或lerna,包装安装在每个文件夹内,即native-appserver与一些常见的devDependencies像typescript,tslint和prettier安装在packages所在的根文件夹中。

项目使用以下tsconfig

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["es2017"],

    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,

    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "alwaysStrict": true,

    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "exclude": [
    "scripts",
    "packages/native-app/dist",
    "packages/server/functions/dist"
  ]
}

并为jazxswpoi打包json

native-app
reactjs typescript react-native
1个回答
7
投票

将React与TypeScript一起使用时,您必须像这样导入反应:

{
  "name": "skimitar-app",
  "version": "1.0.0",
  "description": "Skimitar app",
  "private": true,
  "dependencies": {
    "asq-react-native-device": "1.2.2",
    "asq-react-native-facebook-log-in": "1.1.0",
    "asq-react-native-google-sign-in": "1.2.0",
    "asq-react-native-sensors": "1.1.0",
    "react": "16.4.2",
    "react-native": "0.56.0",
    "react-native-firebase": "4.3.8",
    "react-native-svg": "6.5.2",
    "unstated": "2.1.1"
  },
  "devDependencies": {
    "@types/react": "16.4.13",
    "@types/react-native": "0.56.17"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.