I am using react with typescript I am trying to fetch the data from the API using Axios. I created a state of an array of objects and tried to set the data from the response in Axios then:
const [users, setUsers] = useState<Users[]>([]);
type Users = {
userId: number,
name: string,
}
useEffect(() => {
const response = async () => {
await axios.get(
"https://localhost:5000/users",{
headers: { "Content-Type": "application/json" },
}
);
}
response().then((res) => {
setUsers({userId: res.userId, name: res.name})
})
},[])
I am getting on setUsers saying: Argument of type '{ userId: any; }' is not assignable to parameter of type 'SetStateAction<Users[]>'.
Object literal may only specify known properties, and 'userId' does not exist in type 'SetStateAction<Users[]>