@notifierjs/react v1.1.5
@notifierjs/react
@notifierjs/core binding for react
Table of contents
Installation
@notifierjs/react required @notifierjs/core as a peer dependency, make sure to
install it.
Using npm:
npm install @notifierjs/core @notifierjs/reactUsing yarn:
yarn add @notifierjs/core @notifierjs/reactUsage
First of all we need to create a Notifier instance. For that use useCreateNotifier hook.
import { useCreateNotifier } from '@notifierjs/react';
const notifier = useCreateNotifier();Next you should wrap your app(or part of it) with NotifierProvider
and pass Notifier instance to it.
import { NotifierProvider, useCreateNotifier } from '@notifierjs/react';
import { App } from './App';
export const App = () => {
const notifier = useCreateNotifier<string>();
return (
<NotifierProvider value={notifier}>
<App />
</NotifierProvider>
);
};Then create your notifications render container, where your notifications will be
displayed, you can get notifier instance passed to provider by using
useNotifier hook. For handling timer here we use NotifierTimer component:
import { useNotifier, NotifierTimer } from '@notifierjs/react';
import { Notification } from './Notification';
export const NotificationsContainer = () => {
const notifier = useNotifier<string>();
return (
<>
{notifier.notifications.map((notification) => (
<NotifierTimer notification={notification}>
{(time) => (
<Notification
onClose={() => notifier.remove(notification.id)}
onHover={() => notification.info.timer?.pause()}
onBlur={() => notification.info.timer?.start()}
initialTime={notification.options.autoRemoveTimeout}
time={time}
>
{notification.payload}
</Notification>
)}
</NotifierTimer>
))}
</>
);
};Now you only need to add notification and you're done!
notifier.add({ id: 1, payload: 'Lorem ipsum' });See complete working example here
Examples
Check out examples folder for more complete examples!
State managers
Check this exmaples to see how to connect @notifierjs/core with mobx state manager.
Hooks
useCreateNotifier
Create memoized instance of Notifier interface.
Can receive optional object with options relative to Notifier
import { useCreateNotifier } from '@notifierjs/react';
export const App = () => {
const notifier = useCreateNotifier();
return (
// ...
);
};useNotifierContext
Returns current context value.
import { useNotifierContext } from '@notifierjs/react';
export const App = () => {
const notifier = useNotifierContext();
return (
// ...
);
};useNotifier
Returns binded to react Notifier instance.
import { useNotifier, NotifierTimer } from '@notifierjs/react';
import { Notification } from './Notification';
export const NotificationsContainer = () => {
const notifier = useNotifier();
return (
<>
{notifier.notifications.map((notification) => (
<NotifierTimer notification={notification}>
{(time) => (
<Notification
onClose={() => notifier.remove(notification.id)}
onHover={() => notification.info.timer?.pause()}
onBlur={() => notification.info.timer?.start()}
initialTime={notification.options.autoRemoveTimeout}
time={time}
>
{notification.payload}
</Notification>
)}
</NotifierTimer>
))}
</>
);
};useNotifierTimer
Receiving notification with a timer as input, returns its binded to react value.
import { LaunchedNotification } from '@notifierjs/core';
import { useNotifierTimer } from '@notifierjs/react';
interface Props<Payload> {
children: (time?: number) => React.ReactElement;
notification: LaunchedNotification<Payload>;
}
export const NotifierTimer = <Payload,>({
children,
notification,
}: Props<Payload>): React.ReactElement => {
const time = useNotifierTimer(notification);
return (
// ...
)
};Components
NotifierTimer
Component that receive notification and return render prop with timer that you can use to create notification progress bar.
Props:
| Name | Type | Required | Default |
|---|---|---|---|
notification | LaunchedNotification<Payload>[] | yes | — |
import { useNotifier, NotifierTimer } from '@notifierjs/react';
import { Notification } from './Notification';
export const NotificationsContainer = () => {
const notifier = useNotifier<string>();
return (
<>
{notifier.notifications.map((notification) => (
<NotifierTimer notification={notification}>
{(time) => (
<Notification
onClose={() => notifier.remove(notification.id)}
onHover={() => notification.info.timer?.pause()}
onBlur={() => notification.info.timer?.start()}
initialTime={notification.options.autoRemoveTimeout}
time={time}
>
{notification.payload}
</Notification>
)}
</NotifierTimer>
))}
</>
);
};withNotifierTimer
Same as NotifierTimer but in HOC format. To use it your component
should receive time prop with type number | undefined.
Props:
| Name | Type | Required | Default |
|---|---|---|---|
notification | LaunchedNotification<Payload>[] | yes | — |
And other props that your wrapped component receive.
import { useNotifier, withNotifierTimer } from '@notifierjs/react';
import { Notification } from './Notification';
const TimerNotification = withNotifierTimer(Notification);
export const NotificationsContainer = () => {
const notifier = useNotifier<string>();
return (
<>
{notifier.notifications.map((notification) => (
<TimerNotification
notification={notification}
onClose={() => notifier.remove(notification.id)}
onHover={() => notification.info.timer?.pause()}
onBlur={() => notification.info.timer?.start()}
initialTime={notification.options.autoRemoveTimeout}
>
{notification.payload}
</TimerNotification>
))}
</>
);
};Maintaining
Scripts
List of available scripts to run:
build— build librarytypecheck— run typescript to check typestest— run all teststest:watch— run all tests inwatchmodetest:coverage— run all tests with code coverage output