Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'as' mean when importing a module?

What does it mean when you import something as multiple? so for example,

import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

This is just an example from react router and the javascript documentation only shows example of one declaration after 'as'

It looks like it's importing BrowserRouter as Router, Route and Link so all three variables referred to the same library. If I am right, why would you ever want to do that?

So is it the same as var Router, Route, Link = require('react-router-dom').BrowserRouter();?

like image 342
forJ Avatar asked Mar 20 '17 18:03

forJ


1 Answers

The statement

import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

selectively imports BrowserRouter, Route and Link from react-router-dom. The as Router statement makes BrowserRouter available under the name Router (instead of BrowserRouter). The names of Route and Link are not changed.

It is equivalent to:

const Router = require("react-router-dom").BrowserRouter;
const Route = require("react-router-dom").Route;
const Link = require("react-router-dom").Link;
like image 99
x-ray Avatar answered Sep 21 '22 22:09

x-ray