How to use javascript library like moment.js in

What is the recommended way to use javascript built-in libraries in native response? Are there any specific restrictions?

+49
react-native
Mar 27 '15 at 8:01
source share
4 answers

Easy peasy! From the root of your project, simply run:

npm install moment --save 

Then you can import it into your code:

 import moment from 'moment'; var now = moment().format(); 

Limitations would be what it tries to β€œstretch” to the browser (which does not exist in this context). That's why polyfills stuff like XHR.

The official documentation contains examples of how to use the moments library

+70
Mar 27 '15 at 12:44
source share

Some of the points work in React Native, while others do not. I suspect this is relevant to the audience.

I can use the moment to format:

 moment(new Date()).format("YYYY-MM-DD hh:mm:ss") 

But not for active formatting:

 moment(new Date()).format("YYYY-MM-DD hh:mm:ss").fromNow() 
+3
May 18 '17 at 6:10
source share

It seems that right now, some npm modules are not compatible with the packer . In fact, I did not understand why else, but what I resort to is to have a provider folder and copy it through the web version, but at the top, specially placing

/** * @providesModule moment */

And at the bottom of it change:

module.exports = moment;

Not sure if this is the right way, but the packaging is still pretty new for everyone.

0
Mar 28 '15 at 20:26
source share

to use the npm library just use this command with the appropriate library name

 npm install moment --save 

for example.) npm install {your library name here} --save

then just import into your class and use

 import moment from 'moment'; 
0
Nov 29 '17 at 11:11
source share



All Articles