/react-native-midnight

Subscribe to day change

Primary LanguageJavaMIT LicenseMIT

React Native Midnight

Simple react native package for listening to date changes

CI status codecov semantic-release NPM version NPM downloads

Installation

# with npm
npm install --save react-native-midnight

# with yarn
yarn add react-native-midnight

API

Midnight.addListener

The addListener function connects a function to a day change notification event.

This function then returns the reference to the listener. You can remove the listener by calling the remove method on it.

import Midnight from 'react-native-midnight'

const App = () => {
  React.useEffect(() => {
    const listener = Midnight.addListener(() => {
      Alert.alert('The day has changed')
    })
    return () => listener.remove()
  }, [])

  return <Text>App</Text>
}

useOnDayChange

Convenience hook that calls the passed function when the day changes

useOnDayChange((callback: () => void))

Note: Because useOnDayChange doesn't watch for dependency changes, the listener will be removed and re-added every time this hook is called. Creating your own effect inline with Midnight.addListener might be preferred if this is an issue.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT