vim kndll
vim kndll

Reputation: 19

Concept works apparently in class but using hooks the value is not set in state hooks

the value is not set in state hooks

`

> const Post = (props) => {
>     
>     const [selectedFile, setselectedFile] = React.useState([]);
>     
>     const upload=event=>{
> 
>     setselectedFile(selectedFile,event.target.files[0]);
> 
>     }
>     
>     }`

Upvotes: 0

Views: 26

Answers (2)

vim kndll
vim kndll

Reputation: 19

I finally fix this issue.. Problem is here

const [selectedFile, setselectedFile] = React.useState([]); this is incorrect..

 const [selectedFile, setselectedFile] = React.useState(""); Correct Finally this can fix this issuee

Upvotes: 0

Sujit.Warrier
Sujit.Warrier

Reputation: 2869

you only need to set the current state.

If you want to maintain a list of selected files then do the following

setselectedFile([...selectedFile,event.target.files[0]]);

If you only want to maintain the current file then do this

setselectedFile(event.target.files[0]);

Upvotes: 1

Related Questions