简体   繁体   English

我们应该在使用Create-React-App时导入整个库或特定的单个组件吗?

[英]Should we import a whole library or a specific individual component when using along Create-React-App?

I am asking this on performance/optimizing issue. 我在性能/优化问题上问这个问题。 I realize that all libraries (and their default export method) are different but for example on react-bootstrap's doc intro it states that 我意识到所有库(以及它们的默认导出方法)都不同,但是例如在react-bootstrap的doc介绍中它声明了

You should import individual components like: react-bootstrap/Button rather than the entire library. 您应该导入各个组件,例如:react-bootstrap / Button而不是整个库。 Doing so pulls in only the specific components that you use, which can significantly reduce the amount of code you end up sending to the client. 这样做只会引入您使用的特定组件,这可以显着减少您最终发送到客户端的代码量。

Since we are in 2019, does Create-React-App's (CRA) build automatically optimize for us and only import those components who have been used at least once? 由于我们是在2019年,Create-React-App(CRA)构建是否会自动为我们优化并且仅导入至少使用过一次的组件?

If so, does that mean that we can ignore the advice and import the whole library instead of specific components? 如果是这样,这是否意味着我们可以忽略建议并导入整个库而不是特定组件?

Here are the two examples given by the docs: 以下是文档给出的两个示例:

import Button from 'react-bootstrap/Button';

This is the recommended way to import. 这是推荐的导入方式。 In this case, you are explicitly importing the component from the library. 在这种情况下,您将显式从库中导入组件。

// or less ideally
import { Button } from 'react-bootstrap';

This method imports the entire library and adds the Button component to the scope of that file. 此方法导入整个库,并将Button组件添加到该文件的范围。 This case uses Webpack's tree shaking functionality to reduce the bundle size, which is not entirely reliable. 这种情况使用Webpack的树抖动功能来减少束大小,这不完全可靠。 This article provides insight on why that's the case: https://advancedweb.hu/2017/02/07/treeshaking/ 本文提供了为什么会出现这种情况的见解: https//advancedweb.hu/2017/02/07/treeshaking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM