Does React Native use require or import?

后端 未结 3 1670
暗喜
暗喜 2020-12-30 21:14

Does React Native use require or import?

All I can find is an old tutorial using require(), but when I run react-native

3条回答
  •  时光说笑
    2020-12-30 22:00

    Yes the latest React Native tutorials and examples use the new import syntax.

    https://facebook.github.io/react-native/docs/tutorial.html

    In terms of the differences between CommonJS (require) and ES6 modules (import), there are some good answers here:

    Using Node.js require vs. ES6 import/export

    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.

提交回复
热议问题