In your specific case, you might do the following:
- You need to correct your syntax. Functions are invoked by using
()
following the namespace.
E.g. greet1()
- Create a counter-based state (since it seems like your messages vary based on the "duration"). This allows us to determine the "time" that has passed.
const [downloadStatus, setDownloadStatus] = useState(0);
- Add a
onClick
listener to trigger the download.
const messageDownloadPairs = {
1: ["Message1", 0],
2: ["Message2", 10 * 1000]
3: ["Message3", 20 * 1000]
}
const getDownloadMessageTimePair = (status) =>
messages[Math.min(status, Object.values(messages).length)]
with the following jsx
under return
<button onClick={invokeDownload}>{getDownloadMessage(downloadStatus)[0]}</button>
where invokeDownload
follows the signature
const invokeDownload = () => {
// Your download code here.
setDownloadStatus(1) // Notifying the component that the download has started.
}
- Create a function
checkDownloadStatus
to check if the download has completed. You can find some examples here
- Use the hook
useEffect
to handle timeouts, and state updates.
useEffect(() => {
const downloadComplete = checkDownloadStatus()
if (!downloadComplete) {
const newDownloadStatus = downloadStatus + 1
setTimeOut(
() => setDownloadStatus(newDownloadStatus),
getDownloadMessageTimePair(downloadStatus)[1]
)
}
}, [downloadStatus])
With this, you have a "progress-supported" button to track your csv
download. I'll update this when I get more information on the specifics of your project.