[英]React Native: How to split a file up into multiple files and import them?
I am writing my first app in react native and my js file is getting pretty big. 我正在编写我的第一个应用程序in react native,我的js文件变得非常大。 What is the proper way to split the file up.
拆分文件的正确方法是什么。
If i have something like 如果我有类似的东西
var MyClass = React.createClass({
...
})
Can I save it at myclass.js
and include in by some command in another js file? 我可以将它保存在
myclass.js
并在另一个js文件中包含一些命令吗?
In general you can do the following: 通常,您可以执行以下操作:
var MyClass = React.createClass({
...
)}
module.exports = MyClass;
This way you tell what should be publicly available. 通过这种方式,您可以了解应该公开的内容。
And then, in your former big file you can load the contents like this: 然后,在您以前的大文件中,您可以加载这样的内容:
var MyClass = require('./myclass.js');
Require returns the object that references the value of module.exports. Require返回引用module.exports值的对象。
Here is the updated solution with using the import
statement (in latest React-Native and generally Javascript adhering to ECMAScript6 and later): 以下是使用
import
语句的更新解决方案(最新的React-Native和通常遵循ECMAScript6及更高版本的Javascript):
file1 myClass.js: file1 myClass.js:
export default class myClass {...}
file2 app.js: file2 app.js:
import myClass from './myClass';
This is the basic version using a single default
export. 这是使用单个
default
导出的基本版本。 You can also export named
exports that have to be explicitly listed on import. 您还可以导出必须在导入时明确列出的
named
导出。 For more info see export and import . 有关更多信息,请参阅导出和导入 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.