Hi, I want to download a (small) text file when user clicks a link.
Should I use a Cell for this use case?
Reading the docs, I saw “use a Cell when you want to fetch data”
But, how to create a Cell that triggers when user clicks a link?
I asked ChatGPT how she´d resolve this and this was the answer (pretty impressive to be honest):
const DownloadLink = () => {
const [url, setUrl] = useState('')
const handleClick = async () => {
const response = await fetch('/.redwood/functions/downloadFile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
})
const { url } = await response.json()
setUrl(url)
}
return (
<div>
<button onClick={handleClick}>Download File</button>
{url && (
<a href={url} download="myfile.txt">
Download
</a>
)}
</div>
)
}
export default DownloadLink
This solution does not use a Cell but seems to work. Any comments or suggestions?
Thanks in advance