简体   繁体   English

React预计将在全球范围内推出

[英]React is expected to be globally available

I'm playing with React (@13.3) with babel and webpack. 我正在玩带有babel和webpack的React(@ 13.3)。

I have a component that's defined like this: 我有一个像这样定义的组件:

import BaseComponent from './BaseComponent';

export default class SomeComponent extends BaseComponent {
    render() {
        return (
            <div>
                    <img src="http://placekitten.com/g/900/600"/>
            </div>
        );
    }
}

But I get the following error: 但是我收到以下错误:

Uncaught ReferenceError: React is not defined 未捕获的ReferenceError:未定义React

I understand the error: the JSX bit is compiled into React.createElement(...) but React isn't in the current scope since it's not imported. 我理解错误:JSX位被编译成React.createElement(...)React不在当前范围内,因为它没有被导入。

My questions is: What's the clean way to work around this issue? 我的问题是:解决这个问题的干净方法是什么? Do I have to somehow expose React globally with webpack? 我是否必须以某种方式在Webpack中全局公开React


Solution used: 解决方案:

I followed @salehen-rahman suggestion. 我跟着@ salehen-rahman的建议。

In my webpack.config.js: 在我的webpack.config.js中:

module: {
    loaders: [{
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'react-hot!babel?plugins[]=react-require'
    }, {
        test: /\.css$/,
        loader: 'style!css!autoprefixer?browsers=last 2 versions'
    }]
},

I also needed to fix my tests, so I added this to the file helper.js: 我还需要修复我的测试,所以我将其添加到文件helper.js:

require('babel-core/register')({
    ignore: /node_modules/,
    plugins: ['react-require'],
    extensions: ['.js']
});

My tests are then launched with the following command: 然后使用以下命令启动我的测试:

mocha --require ./test/helper.js 'test/**/*.js'

My questions is : What's the clean way to work around this issue ? 我的问题是:解决这个问题的干净方法是什么? Do I have to somehow expose React globally with webpack ? 我是否必须以某种方式在Webpack中全局公开React?

Add babel-plugin-react-require to your project, and then amend your webpack's Babel config to have settings akin to: babel-plugin-react-require添加到您的项目中,然后修改您的webpack的Babel配置,使其具有类似于以下设置:

loaders: [
  {
    test: /.js$/,
    exclude: /node_modules/,
    loader: 'babel-loader',
    query: {
      stage: 0,
      optional: ['runtime'],
      plugins: [
        'react-require' // <-- THIS IS YOUR AMENDMENT
      ]
    },
  }
]

Now, once you've applied the configuration update, you can initialize React components without manually importing React. 现在,一旦应用了配置更新,就可以初始化React组件而无需手动导入React。

React.render(
  <div>Yippee! No <code>import React</code>!</div>,
  document.body // <-- Only for demonstration! Do not use document.body!
);

Bear in mind though, babel-plugin-react-require transforms your code to automatically include React imports only in the presence of JSX tag in a specific file, for a specific file. 但请记住, babel-plugin-react-require您的代码转换为特定文件中存在JSX标记的情况下自动包含React导入, 以用于特定文件。 For every other file that don't use JSX, but needs React for whatever reason, you will have to manually import React. 对于不使用JSX但由于某种原因需要React的每个其他文件,您必须手动导入React。

If you have react in your node modules directory you can add import React from 'react'; 如果您在节点模块目录中有反应,可以import React from 'react';添加import React from 'react'; at the top of your file. 在您的文件的顶部。

You can use Webpack's ProvidePlugin . 您可以使用的WebPack的ProvidePlugin To use, update the plugins section in your Webpack config to include the following: 要使用,请更新Webpack配置中的插件部分以包含以下内容:

plugins: [
  new webpack.ProvidePlugin({
    'React': 'react'
  })
]

This, however, doesn't solve it for the tests.. 然而,这并没有解决它的测试..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM