react-alert
A simple react alert (toaster style) component.
Demo
You can see a live demo here.
Installation
$ npm install react-alert
Usage
To use it, you have to import the AlertContainer
component, like this:
import React, {Component} from 'react'
import AlertContainer from 'react-alert'
export default class App extends Component {
alertOptions = {
offset: 14,
position: 'bottom left',
theme: 'dark',
time: 5000,
transition: 'scale'
}
showAlert = () => {
this.msg.show('Some text or component', {
time: 2000,
type: 'success',
icon: <img src="path/to/some/img/32x32.png" />
})
}
render () {
return (
<div>
<AlertContainer ref={a => this.msg = a} {...this.alertOptions} />
<button onClick={this.showAlert}>Show Alert</button>
</div>
)
}
}
Api
Once you have the reference of the AlertContainer
you can call the following methods:
this.msg.show('Some message or component')
this.msg.info('Some info message or component')
this.msg.success('Some success message or component')
this.msg.error('Some error message or component')
this.msg.removeAll()
Options
The AlertContainer
component accepts the following props:
{
offset: 14,
position: 'bottom left',
theme: 'dark',
time: 5000,
transition: 'scale'
}
When you call show
, info
, success
and error
method, you can include the following options as a second parameter:
{
time: 0,
icon: <img src="path/to/some/img/32x32.png" />,
onClose: () => {}
}
When you call the show
method, you can additionally include the info
option:
{
type: 'info'
}
Using React Components as alert content
You can also use a React Component to show an alert message, like this:
this.msg.show(<AComponent aProp="some message" />)