【问题标题】:Error rendering react component using Typescript and WebPack使用 Typescript 和 WebPack 渲染反应组件时出错
【发布时间】:2017-09-25 13:41:10
【问题描述】:

我正在尝试构建最简单的反应组件。它只是呈现一个 Hello 页面,但我的控制台中出现错误。我使用打字稿作为我的编译器。我正在尝试按照本指南为我的项目进行设置:https://github.com/Microsoft/TypeScript-React-Starter

错误

Uncaught ReferenceError: React is not defined
    at Object.<anonymous> (external "React":1)
    at __webpack_require__ (bootstrap 2fd03459d628585593a4:19)
    at Object.module.exports (index.tsx:1)
    at __webpack_require__ (bootstrap 2fd03459d628585593a4:19)
    at Object.<anonymous> (bundle.js:3669)
    at __webpack_require__ (bootstrap 2fd03459d628585593a4:19)
    at module.exports.ctor.super_ (bootstrap 2fd03459d628585593a4:62)
    at bootstrap 2fd03459d628585593a4:62

这是我的 WebPack 配置:

module.exports = {
    entry: "./src/index.tsx",
    output: {
        filename: "bundle.js",
        path: __dirname + "/dist",
        publicPath: "/dist/"
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    devServer: {
        headers: {
            'Access-Control-Allow-Origin': '*'
        },
        port: 8282
    },

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" },

            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

这里是 myindex.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";

import { Hello } from "./components/Hello";

ReactDOM.render(
    <Hello compiler="TypeScript" framework="React" />,
    document.getElementById("example")
);

这是我的 hello 组件

import * as React from "react";

export interface HelloProps { compiler: string; framework: string; }

// 'HelloProps' describes the shape of props.
// State is never set so we use the 'undefined' type.
export class Hello extends React.Component<HelloProps, undefined> {
    componentWillMount () {
        if ('pluginLoaded' in window) {
            (window as any).pluginLoaded('hello', function (port: any, context: any) {
                // Future work should interact with the message channel here
            });
        }
    }

    render() {
        return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
    }
}

【问题讨论】:

    标签: reactjs typescript webpack


    【解决方案1】:

    您将reactreact-dom 定义为Externals。正如您在 webpack 配置中 external 选项的评论中所写,预计 对应的全局变量存在

    你必须在你的 HTML 中包含一个 React 版本。通过在 HTML 中包含以下脚本,可以从 CDN 使用 React。

    <script crossorigin src="https://unpkg.com/react@15/dist/react.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
    

    这是 React 的开发版本,如果你打算在生产中使用它,你应该使用缩小版本。更多详情请见React - Using a CDN

    【讨论】:

    • 是的。你说的对。我只是在 Webpack 中注释掉了外部配置。如果我没有将其注释掉,我将不得不将它们包含在您提到的脚本标签中。
    猜你喜欢
    • 2017-02-12
    • 2019-08-02
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2017-06-28
    • 2022-07-04
    • 2019-07-27
    相关资源
    最近更新 更多