I’m trying to upload a picture using react hooks
JavaScript
x
13
13
1
const [picture, setPicture] = useState();
2
3
const onChangePicture = e => {
4
console.log('picture: ', picture);
5
setPicture(picture, e.target.files[0]);
6
};
7
8
<input
9
type="file"
10
//style={{ display: 'none' }}
11
onChange={e => onChangePicture(e)}
12
/>
13
however I’m getting the following error:
JavaScript
1
2
1
Uncaught TypeError: picture is not iterable
2
when I change the onChangePicture to
JavaScript
1
2
1
setPicture(picture, e.target.files[0])
2
the picture variable is undefined,
any help would be appreciated.
Advertisement
Answer
I think you meant to do:
JavaScript
1
2
1
setPicture([picture, e.target.files[0]]);
2
This will concatenate the first file to all current files.
Remember to use const [picture, setPicture] = useState([]);
as to make sure it doesn’t break the first time around