Shell
x
1
1
npx create-react-app toastifyexample
Shell
1
1
1
cd toastify
Shell
1
1
1
npm i react-toastify
Full Example with Source Code
App.js
JavaScript
1
59
59
1
import React, { useState } from 'react'
2
import './App.css'
3
import 'react-toastify/dist/ReactToastify.css'
4
import { toast, ToastContainer } from 'react-toastify'
5
6
function App() {
7
8
const notify = (message, hasError = false) => {
9
if (hasError) {
10
toast.error(message, {
11
position: 'top-center',
12
autoClose: 5000,
13
hideProgressBar: false,
14
closeOnClick: true,
15
pauseOnHover: true,
16
draggable: true,
17
progress: undefined,
18
})
19
} else {
20
toast(message, {
21
position: 'top-center',
22
autoClose: 5000,
23
hideProgressBar: false,
24
closeOnClick: true,
25
pauseOnHover: true,
26
draggable: true,
27
progress: undefined,
28
})
29
}
30
}
31
32
const showAlert = () => {
33
notify("Hello world",true)
34
}
35
36
37
return (
38
<div className='App'>
39
40
<button onClick={showAlert}>
41
Show Alert
42
</button>
43
44
<ToastContainer
45
position='top-center'
46
autoClose={5000}
47
hideProgressBar={false}
48
newestOnTop={false}
49
closeOnClick
50
rtl={false}
51
pauseOnFocusLoss
52
draggable
53
pauseOnHover
54
/>
55
</div>
56
)
57
}
58
59
export default App
Here we have a simple button
to show alert messages. When we click this button the toastify
alert notification will show in the center
position as we had given the position top-center
. Now, if you run the app, you will see something like the below screenshot.

As you can see we are showing the error
notification message. Now, to show the success
notification you need to change the below code in App.js
file.
App.js
JavaScript
1
59
59
1
import React, { useState } from 'react'
2
import './App.css'
3
import 'react-toastify/dist/ReactToastify.css'
4
import { toast, ToastContainer } from 'react-toastify'
5
6
function App() {
7
8
const notify = (message, hasError = false) => {
9
if (hasError) {
10
toast.error(message, {
11
position: 'top-center',
12
autoClose: 5000,
13
hideProgressBar: false,
14
closeOnClick: true,
15
pauseOnHover: true,
16
draggable: true,
17
progress: undefined,
18
})
19
} else {
20
toast(message, {
21
position: 'top-center',
22
autoClose: 5000,
23
hideProgressBar: false,
24
closeOnClick: true,
25
pauseOnHover: true,
26
draggable: true,
27
progress: undefined,
28
})
29
}
30
}
31
32
const showAlert = () => {
33
notify("Data is inserted successfully")
34
}
35
36
37
return (
38
<div className='App'>
39
40
<button onClick={showAlert}>
41
Show Alert
42
</button>
43
44
<ToastContainer
45
position='top-center'
46
autoClose={5000}
47
hideProgressBar={false}
48
newestOnTop={false}
49
closeOnClick
50
rtl={false}
51
pauseOnFocusLoss
52
draggable
53
pauseOnHover
54
/>
55
</div>
56
)
57
}
58
59
export default App

Themes of Notification Messages
Three themes are supported
- dark
- light
- colored
Types of Notification Messages
- error
- warning
- info
- success
- default