What causes a context type error when using axios in React with TypeScript and how to fix it?

huangapple go评论71阅读模式
英文:

What causes a context type error when using axios in React with TypeScript and how to fix it?

问题

我想用 TypeScript 写上下文。因此,通过 axios 获取 API 数据,然后将其写入卡片并显示在屏幕上。但是它会出现有关上下文类型的错误,我该如何消除它?

我的错误是:类型“{ data: ProductList | undefined; setData: React.Dispatch<React.SetStateAction<ProductList | undefined>>; }”无法分配给类型“productType[]”。
对象文字只能指定已知属性,而“data”在类型“productType[]”中不存在。

import axios from "axios";
import { createContext, useEffect, useState } from "react";

interface ContextChildrenProps {
    children: JSX.Element
}

export interface SingleProduct {
    title: string,
    description: string,
    thumbnail: string
}

export interface productType {
    product: SingleProduct
}

export interface ProductList {
    data: productType[]
}

export const GlobalProductsContext = createContext<productType[] | null>({} as productType[])

export const ProductsContextProvider = ({ children }: ContextChildrenProps) => {

    const [data, setData] = useState<ProductList>()

    useEffect(() => {
        axios.get('https://fakestoreapi.com/products')
            .then(res => {
                console.log(res)
                setData(res.data)
            })

    }, [])

    return (
        <GlobalProductsContext.Provider value={{ data, setData }}>
            {children}
        </GlobalProductsContext.Provider>
    )
}
import React from 'react';
import { productType } from '../context/GlobalData';

const SingleCard = ({ product }: productType) => {
    return (
        <>
            <div className="col-lg-4 p-2" data-aos="flip-left">
                <div className="card" style={{ width: '100%', height: '100%' }}>
                    <img src={product.thumbnail} className="card-img-top" alt="..." />
                    <div className="card-body">
                        <h5 className="card-title">{product.title}</h5>
                        <p className="card-text">{product.description}</p>
                        <a href="#" className="btn btn-primary">Go somewhere</a>
                    </div>
                </div>
            </div>
        </>
    )
}

export default SingleCard;
英文:

I want to write context with typescript. So here api is drawn through axios and written to the screen with cards. But it gives an error about the context type, how can I eliminate it?

My Error is : Type '{ data: ProductList | undefined; setData: React.Dispatch<React.SetStateAction<ProductList | undefined>>; }' is not assignable to type 'productType[]'.
Object literal may only specify known properties, and 'data' does not exist in type 'productType[]'.

import axios from &quot;axios&quot;;
import { createContext, useEffect, useState } from &quot;react&quot;;
interface ContextChildrenProps {
    children: JSX.Element
}

export interface SingleProduct {
    title: string,
    description: string,
    thumbnail: string
}

export interface productType {
    product: SingleProduct
}

export interface ProductList {
    data: productType[]
}

export const GlobalProductsContext = createContext&lt;productType[] | null&gt;({} as productType[])

export const ProductsContextProvider = ({ children }: ContextChildrenProps) =&gt; {

    const [data, setData] = useState&lt;ProductList&gt;()

    useEffect(() =&gt; {
        axios.get(&#39;https://fakestoreapi.com/products&#39;)
            .then(res =&gt; {
                console.log(res)
                setData(res.data)
            })

    }, [])

    return (
        &lt;GlobalProductsContext.Provider value={{ data, setData }}&gt;
            {children}
        &lt;/GlobalProductsContext.Provider&gt;
    )
}



<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React from &#39;react&#39;
import { productType } from &#39;../context/GlobalData&#39;

const SingleCard = ({ product }: productType) =&gt; {
    return (
        &lt;&gt;
            &lt;div className=&quot;col-lg-4 p-2&quot; data-aos=&quot;flip-left&quot;&gt;
                &lt;div className=&quot;card&quot; style={{ width: &#39;100%&#39;, height: &#39;100%&#39; }}&gt;
                    &lt;img src={product.thumbnail} className=&quot;card-img-top&quot; alt=&quot;...&quot; /&gt;
                    &lt;div className=&quot;card-body&quot;&gt;
                        &lt;h5 className=&quot;card-title&quot;&gt;{product.title}&lt;/h5&gt;
                        &lt;p className=&quot;card-text&quot;&gt;{product.description}&lt;/p&gt;
                        &lt;a href=&quot;#&quot; className=&quot;btn btn-primary&quot;&gt;Go somewhere&lt;/a&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/&gt;
    )
}

export default SingleCard

<!-- end snippet -->

答案1

得分: 0

上下文值的 TS 类型不正确。

尝试:

import axios from "axios";
import React, { createContext, useEffect, useState } from "react";

interface ContextChildrenProps {
  children: JSX.Element
}

export interface SingleProduct {
  title: string,
  description: string,
  thumbnail: string
}

type ProductContext = {
  data: SingleProduct[];
  setData: React.Dispatch<React.SetStateAction<SingleProduct[]>>
}

export const GlobalProductsContext = createContext<ProductContext>({ data: [], setData: () => { } })

export const ProductsContextProvider = ({ children }: ContextChildrenProps) => {
  const [data, setData] = useState<SingleProduct[]>([])

  useEffect(() => {
    axios.get('https://fakestoreapi.com/products')
      .then(res => {
        console.log(res)
        setData(res.data)
      })

  }, [])

  return (
    <GlobalProductsContext.Provider value={{ data, setData }}>
      {children}
    </GlobalProductsContext.Provider>
  )
}
英文:

The TS type of context value is not correct.

Try:

import axios from &quot;axios&quot;;
import React, { createContext, useEffect, useState } from &quot;react&quot;;

interface ContextChildrenProps {
  children: JSX.Element
}

export interface SingleProduct {
  title: string,
  description: string,
  thumbnail: string
}

type ProductContext = {
  data: SingleProduct[];
  setData: React.Dispatch&lt;React.SetStateAction&lt;SingleProduct[]&gt;&gt;
}

export const GlobalProductsContext = createContext&lt;ProductContext&gt;({ data: [], setData: () =&gt; { } })

export const ProductsContextProvider = ({ children }: ContextChildrenProps) =&gt; {
  const [data, setData] = useState&lt;SingleProduct[]&gt;([])

  useEffect(() =&gt; {
    axios.get(&#39;https://fakestoreapi.com/products&#39;)
      .then(res =&gt; {
        console.log(res)
        setData(res.data)
      })

  }, [])

  return (
    &lt;GlobalProductsContext.Provider value={{ data, setData }}&gt;
      {children}
    &lt;/GlobalProductsContext.Provider&gt;
  )
}

huangapple
  • 本文由 发表于 2023年6月5日 16:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定