Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Class definition vs export default

Tags:

reactjs

jsx

I'm new to React and following https://serverless-stack.com/.

While from the start, components are defined as:

class ComponentName extends React.Component {
...
}

When I got to this step, at the AppliedRoutes section:

export default ({ component: C, props: cProps, ...rest }) =>
  <Route {...rest} render={props => <C {...props} {...cProps} />} />;

Is it correct to say that this is a new component named AppliedRoute, which Parent passes it:

  1. component (component: C)
  2. props (props: cProps)
  3. the rest of the parent props (...props)

How do I go about thinking about this conceptually in the class ComponentName extends React.Component definition in the first example?

like image 723
ali Avatar asked Nov 02 '17 07:11

ali


People also ask

What is the difference between export and export default in React?

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.

What is export default class React?

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 you export a class in React?

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.

Why do we use export in React?

Importing and exporting in React JS will help us write modular code, i.e., splitting code into multiple files. Importing allows using contents from another file, whereas exporting makes the file contents eligible for importing.


2 Answers

You're dealing with two separate issues here. Lets take your component and turn it into a variable

Below is your same function assigned to a MyRoute variable. It is a stateless or "function" component. It is the simplest type of react component because it's just a single function.

const MyRoute = ({ component: C, props: cProps, ...rest }) =>
    <Route {...rest} render={props => <C {...props} {...cProps} />} />;

Now, below is the same component again but as a class. This can have additional functions, a constructor, and can contain state (this.setState()). I stress that this is functionally equivalent to the above, but it can contain state.

class MyRoute extends React.Component {
    render() {
        const { component: C, props: cProps, ...rest } = this.props;
        return (
            <Route {...rest} render={props => <C {...props} {...cProps} />} />
        );
    }
}

Now, to your question about exports, you can export either of these in a few different ways

export { MyRoute }; // access at 'exports.MyRoute'

export default MyRoute; // access at 'exports.default'

export = MyRoute; // access at 'exports'

These are all just different ways to expose your class/variable through the exports object. It doesnt matter what you export, it could be anything. This also affects the way you need to import your class.

The way you posted it in your question is just a shorthand for using a function component and exporting it as the default.

like image 154
caesay Avatar answered Oct 12 '22 22:10

caesay


First example is the standard way of writing React components. If you want to apply the second approach in this case, the final result is something like:

class AppliedRoutes extends React.Component {
  render() {
    const { component: C, props: cProps, ...rest } = this.props

    return (
      <Route
        {...rest}
        render={props => <C {...props} {...cProps} />}
      />
    )
  }
}

The second example is the stateless or functional component. It accepts props as an object argument and returns a React element.

In this case, component, props and rest are all part of AppliedRoutes props. Should your component not handling state nor lifecycle events, just propagating props and returning a new component right away, stick to the second approach.

like image 24
mersocarlin Avatar answered Oct 12 '22 22:10

mersocarlin