import React, { Component } from 'react'; What is the {} part?

时间:2015-09-30 23:19:52

标签: javascript

Pretty simple, can someone explain to me what the part between the curly braces is?

I understand you add it and then you can remove React from say "extends React.Component" but not sure what the use of it is or the reasoning behind it.

1 个答案:

答案 0 :(得分:7)

它基本上只允许您根据需要导入单个成员。在您提供的情况下,它可能没有其他的那么有用。例如:

// constants.js
export const TEST_CONST = 'HOLA';
export const OTHER_TEST_CONST = 'YO';

// someFile.js
import { TEST_CONST } from './constants';

console.log(TEST_CONST); // output: 'HOLA'

希望有所帮助。还对MDN上的模块系统进行了很好的描述。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import