Node.js require vs React.js import

淺唱寂寞╮ 提交于 2019-11-30 20:24:05

问题


I know that in Node.js, when you require some file with variables or functions, from the second time it loaded from the memory.
This concept is the same in import in React.js?
Can i load multiply variables or functions using import, and it will load from the memory in the second time?


回答1:


React is UI library. It isn't related to import keyword. Any available JavaScript features can be used together with React, including import - or require, if it's available.

The question is about ES vs CommonJS modules. The former are universal, the latter are primarily used in Node but also suppored by Webpack and other bundlers, so both import and require can potentially be used in React application.

Can i load multiply variables or functions using import, and it will load from the memory in the second time?

This describes caching behaviour that is specific to all JavaScript modules, including ESM import. A module is evaluated once, the export is cached and can be used on subsequent import.



来源:https://stackoverflow.com/questions/54606962/node-js-require-vs-react-js-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!