Use a controlled progress bar

Imagine you want to see the progress of a file upload. The example below features axios, but it works with anything!

import React from 'react';
import axios from 'axios';
import { toast } from 'react-toastify';
function Example(){
// we need to keep a reference of the toastId to be able to update it
const toastId = React.useRef(null);
function handleUpload(){
method: "post",
url: "/foobar",
data: myData,
onUploadProgress: p => {
const progress = p.loaded /;
// check if we already displayed a toast
if(toastId.current === null){
toastId.current = toast('Upload in Progress', {
progress: progress
} else {
toast.update(toastId.current, {
progress: progress
}).then(data => {
// Upload is done!
// The remaining progress bar will be filled up
// The toast will be closed when the transition end
return (
<button onClick={handleUpload}>Upload something</button>