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

后端 未结 5 2007
臣服心动
臣服心动 2021-02-06 20:13

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

5条回答
  •  别那么骄傲
    2021-02-06 20:33

    If you specifically want to use moment.js in react or react native, have a look at react-moment, a react component for the moment library, at https://github.com/headzoo/react-moment.

    To use react-moment in react native, run:

    npm install --save moment react-moment
    

    Then in the file you want to use moment:

    import Moment from 'react-moment';
    

    Finally, use it as desired, for example:

    
        { post.datePublished }
    
    

    The prop element={Text} is specifically for react native. It ensures that the resulting string is rendered in a Text component. Without this, react native would throw an error.

提交回复
热议问题