Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React is expected to be globally available

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'
like image 954
kombucha Avatar asked Sep 13 '15 19:09

kombucha


People also ask

Can React be installed globally?

You should install it globally in order to create a react project anywhere inside your system. It's not even needed to install create-react-app anymore since you can do npx create-react-app my-app and always use the latest version without polluting your system.

How do you store data globally in React?

Create a file : import React from "react"; const AppContext = {}; export default AppContext; then in App. js, update the value import AppContext from './AppContext'; AppContext. username = uname. value; Now if you want the username to be used in another screen: import AppContext from './AppContext'; AppContext.

Does React need to be imported?

Note: From React version 17 you don't even need to import React from “react” for smaller projects but earlier versions of React projects need to import it. JSX transformer internally takes care of converting a JSX to React elements but you may still need to import it for using hooks like useState and useEffect.

Is React context global state?

React context is nothing but a global state to the app. It is a way to make a particular data available to all the components no matter how they are nested. Context helps you broadcast the data and changes happening to that data, to all the components.


1 Answers

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.

like image 179
Sal Rahman Avatar answered Sep 23 '22 01:09

Sal Rahman