[英]Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React
I am new to React hooks and trying to learn it.我是 React 钩子的新手并试图学习它。 I have a simple get api which is served in heroku server.
我有一个简单的 get api,它在 heroku 服务器中提供。 I have following two components ContactCards and ContactCard in which I have provided the api in ContactCards.
我有以下两个组件 ContactCards 和 ContactCard,其中我在 ContactCards 中提供了 api。
Here I have thumbnai and title as static data where as data1 and data2 are data coming from the api.在这里,我将缩略图和标题作为静态数据,而 data1 和 data2 是来自 api 的数据。
My ContactCard我的名片
import React, { Component } from "react";
interface IContactCardProps {
thumbnail?: string;
title?: string;
data1?: string;
data2?: string;
}
interface IContactCardState {}
class ContactCard extends Component<IContactCardProps, IContactCardState> {
render() {
return (
<div className="contact-card">
<div className="container">
<div className="thumbnail">
<img src={this.props.thumbnail} alt={this.props.title} />
</div>
<div className="title">{this.props.title}</div>
<div className="data">{this.props.data1}</div>
<div className="data">{this.props.data2}</div>
</div>
</div>
);
}
}
export default ContactCard;
My ContactCards我的名片
import ContactCard from "./ContactCard";
import axios from "axios";
function ContactCards() {
const[contacts, setContacts] = useState([])
useEffect(() => {
axios.get('https://texas-crm1.herokuapp.com/api/contactinfo')
.then (res =>{
console.log(res.data[0])
let data = res.data[0];
setContacts ({
contacts: [
{
thumbnail: clock,
title: "Opening Times",
data1: data.open_hour,
data2: data.close_hour,
},
{
thumbnail: telephone,
title: "Phone",
data1: data.phone_1,
data2: data.phone_2,
},
{
thumbnail: location,
title: "Location",
data1: data.street_name,
data2: data.city + ", " + data.state + ", " + data.country,
},
],
});
})
.catch(err =>{
console.log(err)
})
})
return (
<div>
</div>
)
}
Here, I got red underline from contacts in setContacts to all the way down to the closing parenthesis before catch function.在这里,我从 setContacts 中的联系人到一直到 catch 函数之前的右括号都得到了红色下划线。
And the error says the following:错误说明如下:
Argument of type '{ contacts: { thumbnail: string; '{ 联系人:{ 缩略图:字符串; title: string;
标题:字符串; data1: any;
数据1:任何; data2: any;
数据2:任何; }[];
}[]; }' is not assignable to parameter of type 'SetStateAction<never[]>'.
}' 不能分配给类型为 'SetStateAction<never[]>' 的参数。
{ contacts: []}
is an object, your initial state is using []
and that's an array. { contacts: []}
是一个对象,您的初始状态是使用[]
,这是一个数组。 IOW: Your trying to use setState on something that's currently an array, into something that will become an object that contains an array called contacts. IOW:您尝试将 setState 用于当前是数组的内容,将其转换为包含名为 contacts 的数组的对象。
What I would do is create an interface for you useState, based on your IContactCardProps
我要做的是根据您的
IContactCardProps
为您的 useState 创建一个接口
Something like ->类似的东西->
const[contacts, setContacts] =
useState<{contacts:IContactCardProps[]}>
({contacts: []});
Your default setState now is an object with the correct shape for your setContacts
.您的默认 setState 现在是一个具有正确形状的对象
setContacts
。 The advantage here too is that your setContacts
is now type checked, eg.这里的优点也是你的
setContacts
现在被类型检查,例如。 You couldn't accidentally put t1tle: "Phone",
.你不可能不小心把
t1tle: "Phone",
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.