Monday, May 20, 2024
107
rated 0 times [  114] [ 7]  / answers: 1 / hits: 26798  / 8 Years ago, sun, october 9, 2016, 12:00:00

I wanted to double check to make sure I understand imports enough to know if it is ok to do import {_.identity} from 'underscore' opposed to import _ from 'underscore'? That is the only use of underscore if the particular file.



Thank you for your help


More From » ecmascript-6

 Answers
10

Looks like you're very close!



There are a few ways to do this.



IMO the cleanest way to do this goes like this:



import { map, reduce, somethingElse } from 'underscore'


Allowing you to call those methods as so:



map(things, thing => {
...
})


The '{ map, reduce } = ...' part is es6s destructuring assignment.
See the Mozilla docs page for more details on this!



Another way would be to do:



import map from 'underscore/map'
import reduce from 'underscore/reduce'


Personally, I'm not a big fan of this since it can start being a bit more cumbersome as more methods are pulled in but it does have one slight advantage, you can name the reference as you like:



import mappy from 'underscore/map'
import reducerify from 'underscore/reduce'


Though I wouldn't advise using those names!


[#60458] Thursday, October 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
malkajillc

Total Points: 652
Total Questions: 107
Total Answers: 98

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
malkajillc questions
;