Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - when to call react as a variable vs import react

Tags:

reactjs

I work at a company with a large react code base... not all of it is to react standards, and not all of it adheres to it's own standards (im thinking this is pretty standard haha).

I see react being brought into components differently throughout. Here's two examples - marked with (1) and (2):

(1) let React = require('react');
(2) import React, {Component, PropTypes} from 'react';

What is the difference and why use one versus the other? It's not only the react being brought in. I also see import {Component, PropTypes} from 'react'; and let {Component} = React;.

I did a brief search on them internets and couldnt find what i was looking for. maybe my search terms are off a bit. Id be happy with brief explanation and hopefully documentation to go with it. Thank you.

like image 976
Lefty Avatar asked May 04 '17 14:05

Lefty


1 Answers

The difference between the two is that

1) let React = require('react'); 

is ES5 syntax, whereas

2)  import React, {Component, PropTypes} from 'react';

is ES6 syntax

However no Javascript engine as yet supporst ES6 and hence some utility tools like babel behind the scenes convert the ES6 definition to the ES5 syntax of require which @azium says is the Node commonJS syntax for importing modules only.

like image 162
Shubham Khatri Avatar answered Oct 12 '22 23:10

Shubham Khatri