P粉5233350262023-08-16 14:23:10
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]);
P粉7606754522023-08-16 14:11:16
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函数只会更新下一次渲染的状态变量。 如果在调用set函数之后读取状态变量,你仍然会得到在调用之前屏幕上的旧值。
Now, this is my guess, but you can try adding this code:
setSelectedFile(file) setTimeout(() => { console.log(selectedFile); }, 5000);