Skip to content Skip to sidebar Skip to footer

Trying To Understand: Import React, {component} From 'react';

I am working on how to have React.js work with a backend. I have seen this syntax: import React, {component} from 'React'; Why is {component} listed there? In my mind, you would

Solution 1:

React is the default export from the react package, whereas Component is a named export in that package. You can either do

import React from 'react';

classMyClassextendsReact.Component { ... }

or

import React, { Component } from 'react';

classMyClassextendsComponent { ... }

Read more about JavaScript exports here

EDIT: Actually, React uses a different pattern called exposing an API on the default export, which has the same import convention as named exports - you can look at their source here

Solution 2:

Maybe because of this: class MyComponent extends Component {}. class MyComponent extends React.Component {}

In each case, you need Import React from 'react' :)

Post a Comment for "Trying To Understand: Import React, {component} From 'react';"