簡體   English   中英

反應 typescript 得到錯誤為 `Type '() => JSX.Element | 未定義'`

[英]React typescript getting error as `Type '() => JSX.Element | undefined'`

錯誤:

Type '() => JSX.Element | undefined' is not assignable to type 'FC<{}>'.
  Type 'Element | undefined' is not assignable to type 'ReactElement<any, any> | null'.
    Type 'undefined' is not assignable to type 'ReactElement<any, any> | null'.ts(2322)

代碼:

import { FC, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Link, useParams } from "react-router-dom";
import { fetchSingleCocktail } from "../redux/features/cocktailSlice";
import { AppDispatch, RootState } from "../redux/store";

export type ingredientsProps = {
  strIngredient1: string|null;
  strIngredient2: string|null;
  strIngredient3: string | null;
  strIngredient4: string | null;
  strIngredient5: string | null;
}

export type ViewCockTailPros = {
  name: string;
  image: string;
  info: string;
  category: string;
  glass: string;
  instructions: string;
  ingredients:ingredientsProps[]
}

const SingleCocktail:FC = () => {
  const { cocktail } = useSelector((state: RootState) => state.app);
  const [modifiedCocktail, setModifiedCocktail] = useState<ViewCockTailPros|null>();
  const dispatch = useDispatch<AppDispatch>();
  const { id } = useParams();

  useEffect(() => {
    dispatch(fetchSingleCocktail({id}))
  }, [dispatch, id]);

  useEffect(() => {
    if (cocktail && cocktail.length > 0) {
      const {
        strDrink: name,
        strDrinkThumb: image,
        strAlcoholic: info,
        strCategory: category,
        strGlass: glass,
        strInstructions: instructions,
        strIngredient1,
        strIngredient2,
        strIngredient3,
        strIngredient4,
        strIngredient5
      } = cocktail[0];
      
      const ingredients = [strIngredient1, strIngredient2, strIngredient3, strIngredient4, strIngredient5];
      const newCocktail = { name, image, info, category, glass, instructions, ingredients };
      setModifiedCocktail(newCocktail);
    } else {
      setModifiedCocktail(null);
    }
  }, [cocktail]);

  if (!modifiedCocktail) {
      return <h2 className="section-title">No cocktail to display</h2>
  }

  if (modifiedCocktail) { 
    const { name, image, category, info, glass, instructions, ingredients } = modifiedCocktail as ViewCockTailPros;
    return (
        <>
        <section className="section cocktail-section">
                <Link to={"/"}>
                  <button className="btn btn-danger" style={{marginTop:"2rem"}}>Go Back</button>
                </Link>
                <h2 className="section-title">{name}</h2>
                <div className="drink">
                  <img src={image} alt={name} />
                  <div className="drink-info">
                    <p>
                      <span className="drink-data">Name:</span> {name}
                    </p>
                    <p>
                      <span className="drink-data">Category:</span> {category}
                    </p>
                    <p>
                      <span className="drink-data">Info:</span> {info}
                    </p>
                    <p>
                      <span className="drink-data">Glass:</span> {glass}
                    </p>
                    <p>
                      <span className="drink-data">Instructions:</span> {instructions}
                    </p>
                    <p>
                      <span className="drink-data">Ingredents</span> 
                      {ingredients && ingredients.map((item:any, index:number) => {
                        return item ? <span key={index}>{item}</span>:null
                      })}
                    </p>
                  </div>
                </div>
        </section>
        </>
      ) 
  }
}

export default SingleCocktail

有人請幫幫我嗎?

您的 if 語句的構造方式可能無法執行它們,因此 function 可以返回undefined

添加return null; 在組件的末尾,以確保正確的返回類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM