用于React Components的ES6中import语句如何工作 [英] How import statement works in ES6 for React Components

查看:91
本文介绍了用于React Components的ES6中import语句如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PropTypes封装在React源代码中的React对象中,因此该语句的工作方式-

从反应"中导入{PropTypes};

解决方案

模块可以将部分代码导出为默认代码并命名为exports.

例如,react库可能具有类似的内容

// named export
export function PropTypes(){/*....*/}
// defaul export
export default function(){/*....*/}

因此,在导入时,我们可以简单地将默认导出导入为

import React from 'module';

要导入命名的导出,我们应该使用花括号

import {PropTypes} from 'module';

简单地,我们合并以上代码行

import React, { PropTypes } from 'module'

此处

PropTypes is encapsulated in React object in React source code so how this statement is working-

import {PropTypes} from 'react';

解决方案

Modules can export parts of code as default and named exports.

For example, the react library might have something like this

// named export
export function PropTypes(){/*....*/}
// defaul export
export default function(){/*....*/}

So while importing we can import default exports simply as

import React from 'module';

To import named exports we should use curly braces

import {PropTypes} from 'module';

simply we merge the above lines of code

import React, { PropTypes } from 'module'

Read more about modules here

这篇关于用于React Components的ES6中import语句如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆