DEV Community

loading...

Discussion on: You don’t always need to import React

Collapse
karataev profile image
Eugene Karataev

Here's a dirty hack to avoid importing React in every file with JSX. Just add React to window before ReactDOM.render.

import ReactDOM from 'react-dom';
import React from 'react';
import App from './App';

window.React = React;
ReactDOM.render(<App/>, document.querySelector('#mount'));
Enter fullscreen mode Exit fullscreen mode

Now App.js works without throwing a React is not defined error.

// App.js 
export default function() {
  return <div>I am App!</div>
}
Enter fullscreen mode Exit fullscreen mode

This hack is just to illustrate React dependency management. Don't use it in the production code! 😄

Collapse
dance2die profile image
Sung M. Kim Author

Holy mate... Such a convinient hack... You spoiled me 😂

Tried here and worked

Collapse
budyk profile image
Budy

yeep that should do the trick :D, by putting React to the global window object