2.21.0 • Published 3 years ago

react-moment-hooks v2.21.0

Weekly downloads
163
License
MIT
Repository
github
Last release
3 years ago

react-moment-hooks

React hooks for dealing with the Moment.js library.

NPM

Installation

npm install --save react-moment-hooks

Install peer dependencies:

npm install --save react react-dom moment moment-utl

Usage

useLocale hook

import React from "react";
import { useLocale } from "react-moment-hooks";

function Example({ locale }) {
  useLocale(locale, {
    callback: normalizedLocale => {
      // Do something with the loaded normalized locale.
      // This callback will be called when the given locale is either known or unknown.
      //
      // If the given locale is unknown, this function will still be called
      // with "normalizedLocale" set to the default Moment's locale (USA's English, i.e. "en"),
      // but only after calling "unknownLocaleCallback" which will always be called before
      // (this is guaranteed by the underlying "moment-utl" package).
      console.log(normalizedLocale);
    },
    unknownLocaleCallback: (defaultLocale, locale) => {
      // Do something with unknown locale.
      // "defaultLocale" will always be "en" (USA's English).
      // "locale" will be the original locale passed to the hook
      // as first parameter.
      console.log(defaultLocale, locale);
    },
    errorCallback: e => {
      // Do something when locale could not be loaded.
      // If this error callback is not given to "useLocale",
      // then an error will be logged to the console during development (not in production)
      // if an error occurs.
      console.error(e);
    },
  });
  return <div />;
}

export default function App() {
  return (
    <div>
      <Example locale="ru" />
    </div>
  );
}

License

MIT © Anton Bagdatyev (Tonix)

2.21.0

3 years ago

2.19.0

3 years ago

2.20.0

3 years ago

2.18.0

4 years ago

2.17.0

4 years ago

2.16.1

4 years ago

2.16.0

4 years ago

2.15.0

4 years ago

2.13.0

4 years ago

2.14.0

4 years ago

2.11.0

4 years ago

2.12.0

4 years ago

2.10.0

4 years ago

2.9.0

4 years ago

2.8.0

4 years ago

2.7.0

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago