I'm learning React for a week and decided to make a blog for practice. I'm using React hooks and Redux Toolkit.
I want to upload files (images). There's a variable imageUrl:
const [imageUrl, setImageUrl] = useState("");
There's the input:
<input type="file" id="file" accept="image/jpeg, image/png" onChange={(e) => handleImageChange(e)}></input>
And there's a function handleImageChange:
const handleImageChange = e => {
        e.preventDefault();
        let reader = new FileReader();
        let file = e.target.files[0];
        reader.onloadend = () => setImageUrl(reader.result);
        if (file) reader.readAsDataURL(file);
    }
The problem is that setImageUrl doesn't update imageUrl immediately. It works only from the second try. But if I write this code in the event onChange of the input, all works fine.
Why does this happen and how can I fix it?
