Am I using useState incorrectly here?
For context I am attempting to to set up multipart uploads to S3, and yes I did see https://github.com/nramkissoon/t3-s3 and my code is based on that.
I have these two
useState
:
These states get updated based on a trpc call:
But the useState
only saves the data for the last file. From the console.log
I know that I do receive the urls
and the uploadId
, for both the client and server.GitHub
GitHub - nramkissoon/t3-s3: Example create-t3-app with AWS S3 presi...
Example create-t3-app with AWS S3 presigned URL integration - GitHub - nramkissoon/t3-s3: Example create-t3-app with AWS S3 presigned URL integration
3 Replies
console.logs
You want to use a callback inside the setState
the variable that the setState function is mapped to only updates when it's rerendered
setPresignedUrl((e) => [...e, urls]);
setUploadId((e) => [...e, res.uploadId]);
the variable passed by react into the callback function is always up to date
@Mordi hope this helps
It did! Thanks!