The reader.onloadend calls a async function addData and reader.onloadend is in a loop.
const uploadFiles = () => {
console.log(acceptedFiles)
setLoading(true)
console.log(loading)
let i = 0
let l = acceptedFiles.length
for (i = 0; i < l; i++) {
let file = acceptedFiles[i]
const reader = new window.FileReader()
reader.readAsArrayBuffer(file)
reader.onloadend = () => {
let buffer = Buffer(reader.result)
console.log(buffer)
addData(file.path, buffer)
}
}
}
This is my async function addData
async function addData(name, buffer) {
const file = await myfunction()
}
I want to call setLoading(false) after all the async addData in the loop has been called. The onClick function doesn't work, loading is set to false when I click the button before all the async function is done.
const [loading, setLoading] = useState(false)
<button onClick={() => {
uploadFiles()
setLoading(false)
}}
disabled={loading}
>
setLoading(false)after for loop insideuploadFiles