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

48 Views Asked by At

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]);
> 
>     }
>     
>     }`
2

There are 2 best solutions below

2
On

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]);
0
On

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