简体   繁体   中英

How can I use moment() library inside a non-react module (for a React project)?

I'm not sure how to import moment() into my React project. I did yarn add moment but then can't access the library?

I used moment quite frequently. yarn add moment should do.

Next import moment from "moment"

Then you can call moment() anywhere in the file for example:

moment().endOf("year").format("DD/MM/YYYY")

Regardless, if it is a new project, consider avoiding moment and choosing a different library

import moment from 'moment';

...
const Footer = () => {
    return (
      <footer className="main-footer">
        <strong>
          Copyright © {moment().format('Y')}
        </strong>
        All rights reserved.
      </footer>
    );
  };
...
export default Footer;

FYI, moment js is deprecated. Consider using other library, like date-fns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM