React Native 使用需要还是导入? [英] Does React Native use require or import?
问题描述
React Native 使用 require
还是 import
?
Does React Native use require
or import
?
我能找到的只是一个使用 require()
的旧教程,但是当我运行 react-native init
时,我得到了一个使用 的项目导入
.这是因为 React Native 最近发生了变化吗?
All I can find is an old tutorial using require()
, but when I run react-native init
, I'm getting a project that uses import
. Is this due to recent changes in React Native?
主要区别是什么?
推荐答案
是的,最新的 React Native 教程和示例使用新的 import
语法.
Yes the latest React Native tutorials and examples use the new import
syntax.
https://facebook.github.io/react-native/docs/教程.html
关于CommonJS(require)和ES6模块(import)的区别,这里有一些很好的答案:
In terms of the differences between CommonJS (require) and ES6 modules (import), there are some good answers here:
使用 Node.js require 与 ES6 导入/导出一个>
我认为大多数人更喜欢新的 ES6 语法.但是目前没有 JS 引擎实现 ES6 模块,因此需要通过 ES6 转译器(例如 Babel)将其转换为 require
语句.React Native 设置为开箱即用,因此您可以开始使用 import
并且它应该可以正常工作.
I think most people prefer the new ES6 syntax. However no JS engines implement ES6 modules currently, so it needs to be converted by an ES6 transpiler (e.g. Babel) to require
statements. React Native is setup to do this out of the box, so you can just start using import
and it should just work.
这篇关于React Native 使用需要还是导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!