Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use javascript library such as moment.js in react native

Tags:

react-native

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

like image 907
nomnombunty Avatar asked Mar 27 '15 08:03

nomnombunty


People also ask

Can I use MomentJS in React Native?

To use moment. js in React Native, we can install it with NPM. We import it with import moment from 'moment' .

How do I use moment library in react?

Just import the library in the file where you need to format Date or Time. import moment from "moment"; Now your Header must look something like this: After calling the library in the Header section, it's time to format the date and integrate it in the code.

Can you use JavaScript libraries in react?

ReactJS itself is an example of a JavaScript library. But the file structure and coding syntax are a little bit different in ReactJS than in normal vanilla JavaScript. So in this article, we are going to learn how to add an external JavaScript library to a ReactJS Project.

What should I use instead of MomentJS?

There are several libraries out there that can potentially replace Moment in your app. The creators of Moment recommend looking into Luxon, Day. js, date-fns, js-Joda, or even replacing Moment with native JS.


1 Answers

Easy peasy! From the root of your project just run:

npm install moment --save 

Then you can import it in your code:

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

The restrictions would be anything that tries to "reach out" to the browser (which doesn't exist in this context). That's why there's polyfills for things like XHR.

The official documentation has examples on how to use the moment library

like image 169
Colin Ramsay Avatar answered Oct 05 '22 05:10

Colin Ramsay