0

I see a lot of people start a component like:

export default function Example(){XYZ}

Is that the same as writing the function and then exporting at the bottom of the page like so?

function Example(){XYZ};

export default Example
6
  • 1
    The only difference is between a function statement and a function expression, which isn't much at all: stackoverflow.com/q/336859/3001761 Commented Mar 19 at 9:51
  • Are not both examples I gave of declared functions? I.e "function Example(){XYZ}" Commented Mar 19 at 10:14
  • 1
    @jonrsharpe Actually export default function Example() {} has special semantics here and the function is treated as a declaration in this case rather than a function expression. Commented Mar 19 at 10:15
  • 1
    @NickParsons even less difference than I thought then, thanks. Commented Mar 19 at 10:38
  • @Aleks3000 Just to be clear, this is nothing to do with React specifically, it's about JS modules. This is part of standard JS. React may make use of this feature in places, but it's not part of React. Commented Mar 19 at 11:38

1 Answer 1

1

For the most part, there isn't much of a difference here. In both cases, you're creating function declarations and then exporting them. While export default can have an expression following it, export default function Example() {} is handled specially and is treated as a function declaration in this case, just like in your first example. And you can see this as you're still able to call Example - meaning its identifier was added to the current scope (which function expressions do not do).

While you would almost never do this in React code, there is however a small difference:

// Example.js
function Example(){XYZ}
export default Example;
setTimeout(() => {Example = 123;}, 100);

This exports your function as is, meaning that you can be safe in knowing that Example in the consuming module will always be the function reference that you exported:

import Example from "./Example.js";
console.log(Example); // logs the function: ƒ Example() {}
setTimeout(() => {
  console.log(Example); // logs the function: ƒ Example() {} (even though the binding has been re-assigned in the source module)
}, 200);

on the other hand:

// Example.js
export default function Example(){XYZ}
setTimeout(() => {
  Example = 123;
}, 100);
import Example from "./Example.js";
console.log(Example); // logs the function: ƒ Example() {}
setTimeout(() => {
  console.log(Example); // logs the number: 123 (changing the binding in the source impacts this)
}, 200);

In this case, the live binding to Example is exported, and so changing it in the source impacts in it the consuming module - again, rare to do in React code so almost not something you need to worry about. You can read more about this in depth in this great article by Jake Archibald.

Sign up to request clarification or add additional context in comments.

2 Comments

The other (small) difference is that the exported function declaration binding is hoisted, even across modules. This is relevant in case of circular module dependencies (not so much in React, where component imports are rarely used in top-level code), and it is why I would recommend to prefer the (also more concise) export default function Example() {} style. Don't worry about the mutability (which can only be done from the exporting module anyway).
@Bergi true, good point, that's another difference to keep in mind

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.