简体   繁体   English

类型参数不可分配给“SetStateAction”类型的参数<never[]> &#39;。 在反应中

[英]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.

相关问题 Typescript never[] 类型的参数不可分配给 SetStateAction 类型的参数<never[]></never[]> - Typescript Argument of type never[] is not assignable to parameter of type SetStateAction<never[]> ReactJS:“(bullets:never[])=&gt; string[]”类型的参数不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - ReactJS: Argument of type '(bullets: never[]) => string[]' is not assignable to parameter of type 'SetStateAction<never[]>' 'Promise 类型的参数<item | undefined> []' 不可分配给“SetStateAction”类型的参数<item[]> '</item[]></item> - Argument of type 'Promise<Item | undefined>[]' is not assignable to parameter of type 'SetStateAction<Item[]>' “状态”类型的参数不能分配给“从不”类型的参数 - Argument of type 'State' is not assignable to parameter of type 'never' ReactJS:未知类型的参数不可分配给 SetStateAction 类型的参数<string>来自文件阅读器</string> - ReactJS: Argument of type unknown is not assignable to parameter of type SetStateAction<string> from filereader &#39;(prevState: Stock[]) =&gt; (Stock | Stock[])[]&#39; 类型的参数不可分配给 &#39;SetStateAction 类型的参数<Stock[] | []> &#39; - Argument of type '(prevState: Stock[]) => (Stock | Stock[])[]' is not assignable to parameter of type 'SetStateAction<Stock[] | []>' &#39;{ identifier: string; 类型的参数 }&#39; 不可分配给类型为 &#39;ConcatArray 的参数<never> &#39; - Argument of type '{ identifier: string; }' is not assignable to parameter of type 'ConcatArray<never>' “AnyAction”类型的参数不能分配给“never”类型的参数。 无法追踪 - Argument of type 'AnyAction' is not assignable to parameter of type 'never'. unable to track it “Function”类型的参数不能分配给“ComponentType”类型的参数<never> &#39; - Argument of type 'Function' is not assignable to parameter of type 'ComponentType<never>' “any”类型的参数不可分配给“never”类型的参数 typescript 解决方案 - argument of type 'any' is not assignable to parameter of type 'never' typescript solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM