REACT组件将对状态变化授予效果

发布于 2025-01-30 09:51:07 字数 2101 浏览 2 评论 0原文

这是我的代码。 ImageChunks状态变量未正确更新。选择图像返回一个空数组。选择另一个图像返回最后一个图像的base64块的数组。 Setimagechunks()似乎没有触发重新渲染。是什么原因造成的?任何帮助将不胜感激。

const [ imageChunks, setImageChunks ] = useState([])
    
const handleImageChange = async (e) => {    
            const files = e.target.files
    
            // Break image base64 string into chunks for database upload, store each in object with series (filename) and part
            const getChunks = (filename, baseStr, chunkSize) => {
                const str = baseStr.split("")
                const chunks = []
                let part = 0
                let count = str.length
                let start = 0
                while (count){
                    part++
                    start = str.length - count
                    const allowedCount = count < chunkSize ? count : chunkSize
                    chunks.push({
                        series: filename,
                        part: part,
                        data: str.slice(start, start + allowedCount).join("")
                    })
                    count -= allowedCount
                }
                
                return chunks 
            }
    
            // empty imageChunks array to prepare for new file selection
            const fileChunks = []
    
            for (let i = 0; i < files.length; i++){
                // Push file name to product object imageFiles array  
                const fileNames =  formValues.imageFiles
                fileNames.push(files[i].name)
                setFormValues({...formValues, imageFiles: fileNames})
    
                // get array of file chunk objects, push to imageChunks array
                const file = await new Response(files[i]).arrayBuffer()
                const baseString = Buffer.from(file).toString('base64')
                const chunks = getChunks(files[i].name, baseString, 10000)
    
                fileChunks.push(chunks)
            }
    
            setImageChunks(null)
            setImageChunks([...fileChunks])
            console.log(imageChunks)
        }

Here is my code. The imageChunks state variable is not updating correctly. Selecting an image returns an empty array. Selecting another image returns an array of the last image's base64 chunks. It seems that setImageChunks() is not triggering a re-render. What is causing this? Any help would be appreciated.

const [ imageChunks, setImageChunks ] = useState([])
    
const handleImageChange = async (e) => {    
            const files = e.target.files
    
            // Break image base64 string into chunks for database upload, store each in object with series (filename) and part
            const getChunks = (filename, baseStr, chunkSize) => {
                const str = baseStr.split("")
                const chunks = []
                let part = 0
                let count = str.length
                let start = 0
                while (count){
                    part++
                    start = str.length - count
                    const allowedCount = count < chunkSize ? count : chunkSize
                    chunks.push({
                        series: filename,
                        part: part,
                        data: str.slice(start, start + allowedCount).join("")
                    })
                    count -= allowedCount
                }
                
                return chunks 
            }
    
            // empty imageChunks array to prepare for new file selection
            const fileChunks = []
    
            for (let i = 0; i < files.length; i++){
                // Push file name to product object imageFiles array  
                const fileNames =  formValues.imageFiles
                fileNames.push(files[i].name)
                setFormValues({...formValues, imageFiles: fileNames})
    
                // get array of file chunk objects, push to imageChunks array
                const file = await new Response(files[i]).arrayBuffer()
                const baseString = Buffer.from(file).toString('base64')
                const chunks = getChunks(files[i].name, baseString, 10000)
    
                fileChunks.push(chunks)
            }
    
            setImageChunks(null)
            setImageChunks([...fileChunks])
            console.log(imageChunks)
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文