loading...

re: 3 React Interview Questions for Junior Devs VIEW POST

FULL DISCUSSION
 

Thanks for the post, I think all three questions are good to ask on an interview.

Recently I asked myself the question 2. It's true that you need to import React because the JSX <div /> is React.createElement('div') under the hood. But it's not necessary to import React in every file, you can just make React globally available.

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'));

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

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

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

code of conduct - report abuse