'A hook as an array
There is this interface:
export interface msgX{
user:any
msg:any
day:any
time:any
}
Then I define a hook array like this:
const [arrayMsg, setArrayMsg] = useState <msgX> ( )
Because I get data from an API:
axios.get(url+"chat/"+props.ticket).then((resp: { data: any; }) => {
for (let i=0; i<resp.data.length;i++){
arrayMsg.push({user:resp.data[i].user,msg:resp.data[i].msg, day:resp.data[i].day, time:resp.data[i].time})
}
})
But I can not make a push to that array hook.
I can not define a hook like this:
const [arrayMsg, setArrayMsg] = useState <msgX> ([])
To do this:
axios.get(url+"chat/"+props.ticket).then((resp: { data: any; }) => {
for (let i=0; i<resp.data.length;i++){
array.push({user:resp.data[i].user,msg:resp.data[i].msg, day:resp.data[i].day, time:resp.data[i].time})
}
setArrayMsg(array)
})
What can I do?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
