【问题标题】:React-native typescript: Can't find variable: ReactReact-native typescript:找不到变量:React
【发布时间】:2018-05-28 04:27:59
【问题描述】:

慢慢尝试将 typescript 集成到我的 React-Native 项目中。语法和所有适用于 ts + tslint + tsconfig,但我的应用程序无法编译。

我的 Button.tsx:

import * as React from 'react'
import { Text, TouchableOpacity, StyleSheet} from 'react-native'

interface ButtonProps {
    txtColor: string
    bgColor: string
    onPress: (e: any) => void
    children: any
}

class Button extends React.Component<ButtonProps, {}> {

    render () {
        const styles =  StyleSheet.create({
            textStyle: {
                alignSelf: 'center',
                color: this.props.txtColor,
                fontSize: 16,
                fontWeight: '900',
                paddingTop: 10,
                paddingBottom: 10
            },
            buttonStyle: {
                flex: 1,
                borderRadius: 75,
                height: 45,
                backgroundColor: this.props.bgColor
            }
        })

        return (
            <TouchableOpacity onPress={this.props.onPress} style={styles.buttonStyle}>
                <Text style={styles.textStyle}>
                    {this.props.children}
                </Text>
            </TouchableOpacity>
        )
    }
}

export { Button }

它转译成这个,在 Index.js 中导入:

Button.js:

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
exports.__esModule = true;
var react_1 = require("react");
var react_native_1 = require("react-native");
var Button = /** @class */ (function (_super) {
    __extends(Button, _super);
    function Button() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Button.prototype.render = function () {
        var styles = react_native_1.StyleSheet.create({
            textStyle: {
                alignSelf: 'center',
                color: this.props.txtColor,
                fontSize: 16,
                fontWeight: '900',
                paddingTop: 10,
                paddingBottom: 10
            },
            buttonStyle: {
                flex: 1,
                borderRadius: 75,
                height: 45,
                backgroundColor: this.props.bgColor
            }
        });
        return (<react_native_1.TouchableOpacity onPress={this.props.onPress} style={styles.buttonStyle}>
                <react_native_1.Text style={styles.textStyle}>
                    {this.props.children}
                </react_native_1.Text>
            </react_native_1.TouchableOpacity>);
    };
    return Button;
}(react_1.Component));
exports.Button = Button;

在 Index.js 中这样导入:

export * from './Button';

但是,在编译时,我遇到了Button.js:37:16 中的错误:

Can't find variable: React

可能出了什么问题?它是 .js 和 .tsx 文件的混合体吗?但是,它正在导入纯 Javascript 文件,所以我看不出问题出在哪里。

Package.json 依赖项:

  },
  "devDependencies": {
    "@types/react": "16.0.29",
    "jest-expo": "21.0.2",
    "react-native-debugger-open": "0.3.15",
    "react-native-scripts": "1.5.0",
    "react-test-renderer": "16.0.0-alpha.12",
    "remote-redux-devtools": "0.5.12",
    "remotedev-rn-debugger": "0.8.3",
    "redux-logger": "3.0.6"
    "typescript": "2.6.2"
  },
  "dependencies": {
    "axios": "0.17.1",
    "expo": "21.0.0",
    "firebase": "4.7.0",
    "mobx": "3.4.1",
    "mobx-react": "^4.3.5",
    "react": "16.0.0-alpha.12",
    "react-native": "0.48.4",
    "react-native-loading-spinner-overlay": "0.5.2",
    "react-native-modal": "4.1.1",
    "react-redux": "5.0.6",
    "redux": "3.7.2",
    "redux-promise": "0.5.3",
    "redux-thunk": "2.2.0"
  }
}

【问题讨论】:

  • 看起来 react-native 找不到它对 react 的依赖。也许重新安装 react-native 并重试?
  • 但它适用于普通的 .js 文件。只是这个 .tsx 转换为 .js 搞砸了。
  • 从'react'导入反应
  • 试过了,也不行:)

标签: javascript reactjs typescript react-native


【解决方案1】:

我检查了你的代码,我觉得一切都很好。

我认为您使用的 React 不支持 @types/react

yarn add @types/react
//or
npm install --save @types/react

这可能会帮助您在代码中使用 typescript 设置 React。

【讨论】:

  • 我已经为 React 安装了类型,不过谢谢。它用tsc 编译就好了,所以语法不应该是问题
  • 我可以看看你的依赖和开发依赖吗?这可能有助于更好地理解这个问题。
  • 更新了,@Prashoor
  • 检查这个来自 Microsoft github.com/Microsoft/TypeScript-React-Native-Starter/blob/… 的 package.json 这可能会帮助您了解一些情况。
  • 嗨,是的,对不起。问题是我需要将 App.js 放在项目的根目录中,从 /dist 而不是 /src 导入我的 ts 文件的“入口点”——这是有道理的。
猜你喜欢
  • 2018-10-26
  • 1970-01-01
  • 2018-08-02
  • 2021-10-10
  • 2018-05-12
  • 2017-01-24
  • 2019-05-27
  • 2021-11-13
  • 2021-05-31
相关资源
最近更新 更多