0

I'm getting started with reactJS.

import React from 'react';
import ReactDOM from 'react-dom';

var App = ()=>{
   return <div> Hello !!</div>
}

ReactDOM.render(<App />, document.getElementById('root'));

In every tutorial or project, everyone is doing import React from 'react';, but nobody is using React in the current file index.js.

1
  • 1
    So...what's the question? I've guessed at it below, but... Commented Jan 8, 2018 at 7:41

2 Answers 2

3

...but using nowhere React in current file index.js

If the question is "Why does it has import React from 'react'?", the answer is: That source code isn't using React directly, but the version of it once it's transpiled is, because once the JSX is transpiled, that looks like this (if we just transpile the JSX, not the ES2015 stuff):

import React from 'react';
import ReactDOM from 'react-dom';

var App = () => {
   return React.createElement(
      'div',
      null,
      ' Hello !!'
   );
}

ReactDOM.render(React.createElement(App, null), document.getElementById('root'));

Note the use of React.createElement. The JSX <App /> syntax is just sugar for that.

Sign up to request clarification or add additional context in comments.

Comments

1

I think that's probably because when transform jsx syntax, you can actually see for example: <div className="exp"><img /></div>, will transform into:

React.createElement('div', {className: 'exp'}, React.createElement('img'));

So, this will need React object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.