8

I'm playing with React (@13.3) with babel and webpack.

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

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.

My questions is: What's the clean way to work around this issue? Do I have to somehow expose React globally with webpack?


Solution used:

I followed @salehen-rahman suggestion.

In my 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:

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'
Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
kombucha
  • 1,424
  • 1
  • 11
  • 19

3 Answers3

7

My questions is : What's the clean way to work around this issue ? Do I have to somehow expose React globally with webpack ?

Add babel-plugin-react-require to your project, and then amend your webpack's Babel config to have settings akin to:

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.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. For every other file that don't use JSX, but needs React for whatever reason, you will have to manually import React.

Sal Rahman
  • 4,607
  • 2
  • 30
  • 43
2

If you have react in your node modules directory you can add import React from 'react'; at the top of your file.

  • 1
    I could do that, but it feels weird to have an import that's not actually used after. Well it is, but not directly by me :/. – kombucha Sep 14 '15 at 18:50
1

You can use Webpack's ProvidePlugin. To use, update the plugins section in your Webpack config to include the following:

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

This, however, doesn't solve it for the tests..

Koen.
  • 25,449
  • 7
  • 83
  • 78