You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
139:54 error Error: Cannot access refs during render
React refs are values that are not needed for rendering. Refs should only be accessed outside of re
nder, such as in event handlers or effects. Accessing a ref value (the `current` property) during r
ender can cause your component not to update as expected (https://react.dev/reference/react/useRef)
Here is the component-
"use client"import{useState,Fragment,useRef}from"react";import{Dialog,Button,Loading,Input,EmailEditor,Select}from"@repo/ui";importImagefrom"next/image";import{IconBugOff}from"@repo/icon/tabler";import{useForm,SubmitHandler,Controller}from"react-hook-form";importtoastfrom"react-hot-toast";import{SignaturePad,SignatureCanvasRef}from"@siamf/react-signature-pad";import{useUpload}from"@repo/blob";//Essentialsimport{keywords,formats}from"@/components/email-template/email-form-data";//Typesimport{ReceiptFormInput}from"@/server/receipt/input/receipt.input";//Trpcimport{trpc}from"@/trpc/client";//Interfaceimport{TReceipt}from"@/server/receipt/output/receipt.output";interfaceProps{
open: boolean;
onClose: ()=>void;
item: TReceipt;}constEditReceipt=({ open, onClose, item }: Props)=>{//Stateconst[sig,setSig]=useState<boolean>(false);//Trpcconstutils=trpc.useUtils();const{ data, fetchNextPage, isFetchingNextPage, hasNextPage, isLoading }=trpc.receipt.templates.useInfiniteQuery({search: "",limit: 12,},{getNextPageParam: (lastPage)=>lastPage.nextCursor,initialCursor: 1,enabled: !!open});const{ mutate, isPending }=trpc.receipt.update.useMutation({onSuccess: (data)=>{returnutils.receipt.list.refetch().finally(()=>{toast.success(data.message);reset();onClose();})},onError: (error)=>{toast.error(error.message)}});//Stateconst[template,setTemplate]=useState<{templateId: string; previewUrl: string}|null>({templateId: item.templateId||"",previewUrl: item.previewUrl||""});//Uploadconst{ upload, isUploading }=useUpload({onError: (error)=>{toast.error(error.message)}});//Form Initializingconst{
register,
handleSubmit,formState: { errors },
reset,
control
}=useForm<ReceiptFormInput>({values: {tmlStr: item?.tmlStr||"",subject: item?.subject||"",dateFormat: item?.dateFormat||"",booleanValue: item?.booleanValue||"",brandId: item.brandId||""}});//Refconstref=useRef<SignatureCanvasRef>(null);//DataconstrawData=data?.pages.flatMap(a=>a.data);//HandlerconstonSubmit: SubmitHandler<ReceiptFormInput> = async (value) =>{letsigUrl: string;if(sig){const{ blob }=awaitupload(ref.current?.toFile());sigUrl=blob?.url||""}else{
sigUrl =item.signature}if(template){mutate({
...value,templateId: template.templateId,previewUrl: template.previewUrl,signature: sigUrl,id: item.id});}}
return (
<Dialogopen={open}onClose={onClose}className="w-[740px] md:w-[740px] lsm:w-[600px] msm:w-[500px] sm:w-[420px] xxs:w-[320px]"><Dialog.Headertitle="Add a receipt"onClose={onClose}className="px-5 py-4"/><hrclassName="border-stroke-light"/><Dialog.BodyclassName="px-5 py-4">{!template&&<Fragment><h4className="text-xl font-semibold text-strong">Choose Template</h4>{isLoading&&<divclassName="my-6 text-center"><LoadingclassName="mx-auto"/><pclassName="text-grayed">Please wait...</p></div>}<divclassName="grid grid-cols-4 md:grid-cols-4 msm:grid-cols-3 xxs:grid-cols-2 gap-x-7 gap-y-10 mt-4">{rawData?.map((item)=>(<divkey={item.id}className="relative cursor-pointer"onClick={()=>setTemplate({previewUrl: item.imageUrl,templateId: item.templateId})}><Imagesrc={item.imageUrl}width={595}height={841}alt={item.name}className="border border-solid border-stroke/60 rounded-2xl"/><h4className="text-center mt-1.5">{item.name}</h4></div>))}</div>{rawData?.length===0&&<divclassName="text-center"><IconBugOffclassName="text-error mx-auto"size={30}/><h4className="text-lg font-semibold">No Template Found</h4><pclassName="font-light">No available templates here, please wait to be uploaded some templates</p></div>}{hasNextPage&&<divclassName="mt-10 text-center"><ButtonisPending={isFetchingNextPage}onClick={()=>fetchNextPage()}>
Load More
</Button></div>}<ButtonclassName="mt-8"onClick={()=>setTemplate({previewUrl: item.previewUrl,templateId: item.templateId})}>
Cancel now
</Button></Fragment>}{template&&<Fragment><buttonclassName="text-main underline underline-offset-2"onClick={()=>setTemplate(null)}>
Change Template
</button><formonSubmit={handleSubmit(onSubmit)}className="mt-6"><Inputid="subject"label="Email Subject"placeholder="Subject"{...register("subject",{required: "Email subject is required"})}errorMessage={errors.subject?.message}/><Controllercontrol={control}name="tmlStr"rules={{required: "Template message is required"}}render={({field: { onChange, value }})=>(<EmailEditorid="message"label="Message"placeholder="Write your message here..."value={value}onChange={onChange}rows={7}words={keywords}errorMessage={errors.tmlStr?.message}/>)}/>{sig&&<Fragment><h4className="text-base font-medium mb-3 inline-block gap-x-1.5"><span>Add your signature</span><spanclassName="text-main underline underline-offset-2 cursor-pointer select-none"onClick={()=>setSig(false)}>(Cancel)</span></h4><divclassName="bg-white mt-2 rounded-xl border border-stroke/50 mb-4"><SignaturePadcanvasProps={{className: "w-full h-[140px] bg-transparent"}}ref={ref}/></div></Fragment>}{!sig&&<divclassName=""><h4className="text-base font-medium mb-3 inline-block gap-x-1.5"><span>Previously added signature</span>{" "}<spanclassName="text-main underline underline-offset-2 cursor-pointer select-none"onClick={()=>setSig(true)}>(Click here to change)</span></h4><Imagesrc={item.signature}width={200}height={120}alt="Signature"/></div>}<h4className="text-lg font-medium mb-5 mt-6">Placeholder Settings</h4><Controllercontrol={control}name="dateFormat"render={({field: { onChange, value }})=>(<Selectlabel="Date Format"placeholder="Choose a date formate for rendering date"onChange={onChange}value={value}options={formats}/>)}/><Controllercontrol={control}name="booleanValue"render={({field: { onChange, value }})=>(<Selectlabel="Boolean Value"placeholder="Choose a value for boolean field"onChange={onChange}value={value}options={[{value: "on/of",label: "On/Off"},{value: "yes/no",label: "Yes/No"}]}/>)}/><Buttontype="submit"isPending={isPending||isUploading}>
Update Now
</Button></form></Fragment>}</Dialog.Body></Dialog>
);
};exportdefaultEditReceipt;
I am not calling ref during render time. Its on a onSubmit handler. Is onSubmit will call render time.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
A linting error I am getting-
Here is the component-
I am not calling ref during render time. Its on a
onSubmithandler. Is onSubmit will call render time.Beta Was this translation helpful? Give feedback.
All reactions