2

I'm new to react native and found two include statements with somewhat same functionalities; require() and import.

What are the main purpose of using these two include module statements and their best practices?

currently, i'm using import on top of the .js file and using require() in any function where i need that module. Is this the correct way to do so??

HungrySoul
  • 1,151
  • 2
  • 17
  • 31

1 Answers1

2

Import does the same thing as require().

Import is an ES6 syntax while require() is common javascript.

See this post for more information.

Edit: you can safely use ES6 import/exports, i suggest you use only this syntax.

Usage with ES6 Import :

Export:

export class/const/func myFunc(){ dosmth };

Import :

Import { myFunc } from "./folder/file";

Export default :

export default myFunc;

Import :

Import myFunc from "./folder/file";

About best practice for React Native :

Prefer writing one component per file, export it by default.

Generally, if you have 1 export in a file do a default export.

If you have more do normal exports.

Always do all your imports at top of the file, from dependencies first then you local imports.

Dyo
  • 4,429
  • 1
  • 15
  • 30