Skip to content

Commit 322833b

Browse files
drochetticpojer
authored andcommitted
add documentation about .native.js extensions (facebook#346)
* add documentation about `.native.js` extensions Lots of projects use the `.native.js` to share code between ReactJS and React Native (including core projects, like Jest) but there's no official documentation about it. I'm adding some info so projects can easily discover and refer to this feature. * fix comments and add pro-tip about `.native.js` Fixed the comment on a code block and added a pro-tip telling users to ignore `.native.js` extensions in their web bundlers in order to optimize final bundle size. * Update platform-specific-code.md
1 parent db56d67 commit 322833b

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎docs/platform-specific-code.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,26 @@ BigButton.android.js
9898
You can then require the component as follows:
9999

100100
```javascript
101-
const BigButton = require('./BigButton');
101+
import BigButton from './BigButton';
102102
```
103103

104104
React Native will automatically pick up the right file based on the running platform.
105105

106-
If you share your React Native code with a website, you might as well use the `BigButton.native.js` so that both iOS and Android will use this file, while the website will use `BigButton.js`.
106+
## Native-specific extensions (i.e. sharing code with NodeJS and Web)
107+
108+
You can also use the `.native.js` extension when a module needs to be shared between NodeJS/Web and React Native but it has no Android/iOS differences. This is specially useful for projects that has common code shared among React Native and ReactJS.
109+
110+
For example, say you have the following files in your project:
111+
112+
```sh
113+
Container.js # picked up by Webpack, Rollup or any other Web bundler
114+
Container.native.js # picked up by the React Native bundler for both Android and iOS (Metro)
115+
```
116+
117+
You can still require it without the `.native` extension, as follows:
118+
119+
```javascript
120+
import Container from './Container';
121+
```
122+
123+
**Pro tip:** Configure your Web bundler to ignore `.native.js` extensions in order to avoid having unused code in your production bundle, thus reducing the final bundle size.

0 commit comments

Comments
 (0)