React Native: How to split a file up into multiple files and import them?

Patrick Klitzke picture Patrick Klitzke · Oct 3, 2015 · Viewed 13.2k times · Source

I am writing my first app in react native and my js file is getting pretty big. 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?

Answer

Oswin Noetzelmann picture Oswin Noetzelmann · Apr 15, 2017

Here is the updated solution with using the import statement (in latest React-Native and generally Javascript adhering to ECMAScript6 and later):

file1 myClass.js:

export default class myClass {...}

file2 app.js:

import myClass from './myClass';

This is the basic version using a single default export. You can also export named exports that have to be explicitly listed on import. For more info see export and import.