This is because when you call or log it in the console, your status has not been updated. You can log your status in the useEffect
hook to view it when updated. Here is an example:
useEffect(() => { console.log("文件 >> ", selectedFile); }, [selectedFile]);
I think your code works as expected, but when you try to call it, the state hasn't been updated yet.
According to React official documentation:
set函數(shù)只會更新下一次渲染的狀態(tài)變量。 如果在調(diào)用set函數(shù)之后讀取狀態(tài)變量,你仍然會得到在調(diào)用之前屏幕上的舊值。
Now, this is my guess, but you can try adding this code:
setSelectedFile(file) setTimeout(() => { console.log(selectedFile); }, 5000);