I have a React component, i need clear this input file but still problem in typescript
The left-hand side of an assignment expression may not be an optional property access.
import { IonButton, IonItem, IonLabel, useIonLoading } from '@ionic/react';
import axios from 'axios';
import React, { FormEvent, useRef, useState } from 'react';
import { errorMessage, successMessage } from '../../services/alertService';
interface ModalAddWorksheetState {
isOpen: boolean;
dismiss: () => void;
CheckerId: string;
regNo: string;
}
const ModalAddWorksheet: React.FC<ModalAddWorksheetState> = ({ isOpen, dismiss, CheckerId, regNo }) => {
const [presentLoading, dismissLoading] = useIonLoading();
const [inspectionPicture, setInspectionPicture] = useState<Blob>();
const inspectionPictureRef = useRef<HTMLInputElement>(null);
const handleImageChange = (e: FormEvent<HTMLInputElement>) => {
setInspectionPicture(e.currentTarget.files![0]);
}
const handleAddWorksheetDetailTemp = async () => {
try {
presentLoading({
message: 'Loading...'
});
const formData = {}
const response = await axios.post(`${process.env.REACT_APP_HOST}/api`, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
if (response.status === 200) {
await successMessage('Success Add Inspection Item');
dismissLoading();
inspectionPictureRef.current?.value = "";
}
} catch (err) {
await errorMessage(err);
dismissLoading();
}
}
const handleSubmitWorksheetDetail = async (e: React.FormEvent) => {
try {
e.preventDefault();
await handleAddWorksheetDetailTemp();
} catch (err) {
errorMessage(err);
}
}
return (
<form onSubmit={handleSubmitWorksheetDetail} encType='multipart/form-data'>
<IonItem>
<IonLabel position='stacked'>Inspection Picture</IonLabel>
<input type={'file'} ref={inspectionPictureRef} onChange={handleImageChange} className="w-full p-2 mt-3 rounded-sm ring-1 ring-slate-300" accept='.png,.jpg,.jpeg' />
</IonItem>
<IonButton type='submit' expand='block'>Submit</IonButton>
</form>
)
}
export default ModalAddWorksheet;
But this code
inspectionPictureRef.current?.value = "";
return error "The left-hand side of an assignment expression may not be an optional property access.ts(2779)"
2条答案
按热度按时间bgtovc5b1#
问题就在这里:
根据the MDN docs,尝试为可选链接表达式的结果赋值是无效的。
换句话说,赋值时不应在左侧使用
?
。请改为尝试执行以下操作,但左侧不包含
?
:说明:
l0oc07j22#
您可以使用下面的remove函数来清除输入字段中的数据,而不是为“当前”赋值。