Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export (default) class in ReactJS

If I'm creating a component, it seems you can create a class in a lot of different ways. What is the difference between these? How do I know which one to use?

import React, {Component} from 'react'  export default class Header extends Component {  }  export const Header = React.createClass({  })  export default React.createClass({  }) 

I'm just assuming they do different things, or is it just different syntax?

If someone could give me a quick explanation, or a link, I would really appreciate it. I don't want to start out with a new framework not knowing exactly what the difference is.

like image 301
Cheese Puffs Avatar asked Jan 17 '16 16:01

Cheese Puffs


People also ask

What is export default in ReactJS?

export default is used to export a single class, function or primitive from a script file. The export can also be written as export default class HelloWorld extends React.Component { render() { return <p>Hello, world!</

Can we export class in react JS?

React uses the same features as mentioned above, and you may treat each React Component as a module itself. Thus, it is possible to import/export React Components and is one of the basic operations to be performed. In React we use the keyword import and from to import a particular module or a named parameter.

What is difference between export default and export in react JS?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.


1 Answers

Hi and welcome to React!

I think part of what you're having trouble with here is not really React-specific, but instead related to the new ES2015 module syntax. When creating React class components, for most intents and purposes you can think of React.createClass as functionally equivalent to class MyComponent extends React.Component. One is just using the new ES2015 class syntax whereas the other is using the pre-ES2015 syntax.

For learning about modules, I'd recommend reading a few articles on the new module syntax to get familiar with it. Here's a good one to start with: http://www.2ality.com/2014/09/es6-modules-final.html.

So in short, these are just differences in syntax, but I'll attempt to do a quick walk-through:

/**  * This is how you import stuff.  In this case you're actually   * importing two things:  React itself and just the "Component"   * part from React.  Importing the "Component" part by itself makes it  * so that you can do something like:  *  * class MyComponent extends Component ...  *   * instead of...  *   * class MyComponent extends React.Component  *   * Also note the comma below  */ import React, {Component} from 'react';   /**  * This is a "default" export.  That means when you import   * this module you can do so without needing a specific module  * name or brackets, e.g.  *   * import Header from './header';  *  * instead of...  *  * import { Header } from './header';  */ export default class Header extends Component {  }  /**  * This is a named export.  That means you must explicitly  * import "Header" when importing this module, e.g.  *  * import { Header } from './header';  *  * instead of...  *   * import Header from './header';  */ export const Header = React.createClass({  })  /**  * This is another "default" export, only just with a   * little more shorthand syntax.  It'd be functionally   * equivalent to doing:  *  * const MyClass = React.createClass({ ... });  * export default MyClass;  */ export default React.createClass({  }) 
like image 181
Nick Ball Avatar answered Sep 17 '22 19:09

Nick Ball