question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: useState won't update ui when FileList type object set

See original GitHub issue

I use useState hook to track files change in html input element. When the file changes, console can output correctly when change event fires. but setFileList not work correctly,useEffect didn’t be called and UI did’d be updated.

the first time I choose a image works fine, but the second time works not correctly

React version:react@16.12.0


    const [fileList, setFileList] = useState<FileList | null>()

    const handleImageChange: ChangeEventHandler<HTMLInputElement> = (e) => {
        const { target: { files } } = e
        setFileList(files)
    }

    useEffect(() => {
        console.log('file list changed: ', fileList)  // nothing happened

    }, [fileList])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
yuquanwangcommented, Feb 23, 2020

@Ayman-Moukhtar Yes, need to point out that, e.target.files and [...e.target.files] are different types, the former is a FileList other than Array.

0reactions
aymanmoukhtarcommented, Feb 23, 2020

@yuquanwang You’re right, i was able to reproduce on firefox version 73.0.1, the reason is that firefox re-uses the same array for appending/removing the newly added files to the input element. as a result:

// In handleChange function.
e.target.files === fileList  // true

That’s why react doesn’t re-render, as the state didn’t change basically, so there is no need - from react’s perspective - for the component to re-render. As your workaround suggested, you would be fine just changing the array reference setFileList([...e.target.files]);

Read more comments on GitHub >

github_iconTop Results From Across the Web

The useState set method is not reflecting a change immediately
Component or React.PureComponent , the state update using the updater provided by useState hook is also asynchronous, and will not be reflected immediately....
Read more >
Steps to Solve Changes Not Reflecting When useState Set ...
Methods to solve the error when the useState set method is not reflecting an immediate change: · Using the “useEffect” hook: · Try...
Read more >
Using the FileReader API to preview images in React
The FileReader API can be tricky to use, but it provides a handy feature that allows users to preview images before uploading to...
Read more >
How to Upload Files With React - Code Frontend
Uploading a single file · First, we add an input element with type="file" attribute. · We can store the selected file in React...
Read more >
Upload - Ant Design
Use defaultFileList for uploaded files when page init. expand code ... by configuring fileList . You can accomplish all kinds of customed functions....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found