What is the major benefit of writing
import React, { Component } from 'react';
class Link extends Component {
...
}
instead of
import React from 'react';
class Link extends React.Component {
...
}
when it comes to react 15.4.x??
In my perspective and in my case (correct me if I am wrong) it does not matter at all since:
webpack2
for making my bundles;webpack.optimize.CommonsChunkPlugin
plugin with minChunks: Infinity
setting to make sure that all vendor code is included only once.From understanding how ES6 imports work I understand that by using named import of {Component}
I state that I want to use only Component
component in my code, which looks.. cleaner.
But since whole React
package is still used in the app, I can create my classes with extension from React.Component
instead of just Component
and in result webpack will still produce the same amount of code and my bundle size will be the same in both cases.
Am I correct?
Explanations: The JSX gets internally into many React. createElement() function calls and each of them returns an object as shown above. Now because of this, we need to import React from “react” since internally every JSX is creating a React Component using JSX transformer.
ReactJS provides reusable components that developers have the authority to reuse and create a new application . Reusability is exactly like a remedy for developers. This platform gives the developers the authority to reuse the components build for some other application having the same functionality.
It allows writing custom components React comes with JSX, an optional syntax extension, which makes it possible to write your own components. These components basically accept HTML quoting and also makes all subcomponent rendering a delightful experience for developers.
You can think of curly braces as creating a portal into JS functionality in your JSX! Because JSX is included in the React library, it won't work if you don't have import React from 'react' at the top of your file!
There is no difference, React.Component
is the same object as Component
, the second way is more eloquent in my opinion, because it really explains that you are using the Component
object from the React
library.
The first one seems to refer a member,
but, it comes from the pre modules era
of javascript, where everything had to be attached to the exported global namespace (just to avoid global namespace pollution).
something that could be under the hood:
// this should be assumed as an example only.
class React { ... }
class Component { ... }
React.Component = Component;
// ES6
export {Component}
export default React;
// ES5
window.React = React;
Note: as someone said, you also need to import React
because JSX
needs to have it on scope, but, if you want to avoid it, you can expose React
globally (window.React = React
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With