Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 export bugg. #16750

Closed
Aleksandar-FFWD opened this issue Nov 8, 2017 · 4 comments
Closed

ES6 export bugg. #16750

Aleksandar-FFWD opened this issue Nov 8, 2017 · 4 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@Aleksandar-FFWD
Copy link

Aleksandar-FFWD commented Nov 8, 2017

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

yes

Environment

Environment:

  • OS: Windows 10
  • Node: 6.10.2
  • Yarn: 0.24.5
  • npm: 5.0.3
  • Watchman: Not Found
  • Xcode: N/A
  • Android Studio: Version 2.3.0.0 AI-162.4069837

Packages: (wanted => installed)

  • react: 16.0.0-alpha.12 => 16.0.0-alpha.12
  • react-native: 0.48.4 => 0.48.4

Steps to Reproduce

Expected Behavior

Expected behavior would be for console.log(mockNews) to return undefied

Actual Behavior

So I was playing around with mock data for the app when I noticed unexpected behavior while exporting .js files

If you export the file like so:

export default mockNews = [{ ID: 0, Title: Lorem Ipsum' }];

When you run console.log(mockNews ) it is available in every single file in the project even if it is not imported.

However if you export a variable like:

var mockNews = [{ ID: 0, Title: Lorem Ipsum' }];

export default mockNews;

Now the behavior is normal. You only have the mockNews if you import the file.

Seams to me that it is not an expected behavior or I'm missing something.

Reproducible Demo

@dawsbot
Copy link

dawsbot commented Nov 10, 2017

Hey @Aleksandar-FFWD, want to clarify how "export default" works in JavaScript. From what I can tell, this is not a bug, it's a feature of how export works.

You can read about it online more, but essentially by using "export default", you are allowing any other file which import's this es6 module to have access to mockNews as the default import. So to consume it you could do

import mockNews from '<direct filepath to that default export file>';

console.log(mockNews);

You need to first import mockNews to be able to use it in each and every file you'd like to consume it in.

@dawsbot
Copy link

dawsbot commented Nov 10, 2017

Assuming this issue can be closed at this point.

@ide
Copy link
Contributor

ide commented Nov 10, 2017

@dawsbot is right, this is just how JS works. The name of default exports is default and assignments are expressions in JS so you're defining a global.

@ide ide closed this as completed Nov 10, 2017
@Aleksandar-FFWD
Copy link
Author

Hey, @dawsbot. Problem is when I'm exporting the file with export default mockNews= {}, I do not have to import mockNews from 'path-to-file' to be able to use it, it is set as a global variable; I can access mockNews anywhere in the project.

is that expected behavior or not?

@facebook facebook locked as resolved and limited conversation to collaborators Nov 10, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Nov 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants