Can you import multiple objects from a library at once?

Question

In the context of this exercise, can you import multiple objects from a library at once?

Answer

Yes, you can do this using “unpacking” when importing from a library.

For example, say that you wanted to import multiple functions from some library, you could do so as follows,

import { functionA, functionB, functionC } from 'library';

Another, more concrete, example is importing objects from the ‘react-router’ library.

import { Router, Switch } from 'react-router';
6 Likes

As in the exercise we are not mentioning which functions to import, doesn’t that import all the functions from react library?

2 Likes

Do we have to maintain a strict name of ‘React’ for the variable or can we use any name?

2 Likes

See the following:

https://www.codecademy.com/courses/introduction-to-javascript/lessons/modules/exercises/import-as?action=resume_content_item

If you’re using the JS import method then you have to use the name ‘React’. If you want to use an alias then you have to write something like:

import { functionA as funcA } from ‘library’;

1 Like

Actually, we are mentioning which part of the library to import when we write:

import React from 'react';

This code imports the React object of the react library. But, wait! There’s no curly braces in it. Then, it’s actually about the type of the export.

It can be read further from:
https://discuss.codecademy.com/t/why-sometimes-import-has-and-sometimes-doesnt/384837

1 Like