我有以下元件,可以呈現文件上傳的可點擊連結
import { gql, useLazyQuery, useMutation } from '@apollo/client'; import { useEffect, useState } from 'react'; import type FileUpload from '@app/data/models/FileUpload'; import { ExternalLink } from '@app/mcui/components/atoms/business-process/shared/ExternalLink'; import { isNonEmptyString } from '@divvy-homes/utils'; export type Props = { uploadId: FileUpload['id']; }; const FILE_UPLOAD_QUERY = gql` query ($id: UUID!) { getFileUpload(id: $id) { id fileName url } } `; const SIGN_FILE_MUTATION = gql` mutation ($url: String!) { signAdminUploadUrl(url: $url) } `; export const FileUploadLink = ({ uploadId }: Props) => { const [fileUrl, setFileUrl] = useState<string>(); const [fileName, setFileName] = useState<string>(); const [getFileData] = useLazyQuery<{ getFileUpload: { url: string; fileName: string; }; }>(FILE_UPLOAD_QUERY, { onError: console.error, onCompleted: (data) => { setFileName(data.getFileUpload.fileName); setFileUrl(data.getFileUpload.url); }, variables: { id: uploadId, }, }); useEffect(() => { void getFileData({ variables: { uploadId } }); }, [getFileData, uploadId]); const [createSignedDocumentUrl] = useMutation<{ signAdminUploadUrl: string }>( SIGN_FILE_MUTATION, { onError: console.error, onCompleted: (urlData) => { const signedUrl = urlData.signAdminUploadUrl; window.open(signedUrl, '_blank', 'noreferrer'); }, }, ); return isNonEmptyString(fileUrl) ? ( <ExternalLink onClick={() => void createSignedDocumentUrl({ variables: { url: fileUrl } })} text={fileName ?? ''} /> ) : undefined; };
每次我使用此元件上傳檔案時,都會收到反應錯誤渲染的鉤子比上次渲染期間更多。
。 React 表示 hooks 的順序發生了以下變化
client.js:1 Warning: React has detected a change in the order of Hooks called by BusinessProcessDetails. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks Previous render Next render ------------------------------------------------------ 1. useContext useContext 2. useContext useContext 3. useContext useContext 4. useContext useContext 5. useRef useRef 6. undefined useState
查看我的程式碼,我無法理解為什麼會發生此錯誤。 React 說是呼叫 useState 的元件的第一行導致了錯誤,但這對我來說毫無意義。沒有條件呼叫反應鉤子會導致這種情況,並且拋出此錯誤的元件的第一行與鉤子更改的順序不符。
我正在使用react 18.2.0,typescript 4.9.5
P粉1484347422023-09-10 20:47:31
事實證明問題出在元件的呼叫方式。呈現 FileUploadLink
的父元件看起來像這樣
export const MyComponent = ({ fileUpload }: MyProps) => { const values = [ { label: 'MyLabel', value: fileUpload ? FileUploadLink({ uploadId: fileUpload }) : undefined, }, ]; return ( <> {values.map((l, v) => { <div>{l}</div>; { v; } })} </> ); };
修復方法是將元件包裝在標籤中,即
const values = [ { label: 'MyLabel', value: fileUpload ? <FileUploadLink uploadId={fileUpload} /> : undefined, }, ];