Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  61] [ 7]  / answers: 1 / hits: 7364  / 3 Years ago, tue, december 21, 2021, 12:00:00

I have this Upload component that I got from antd : react ant design documentation


<Upload
beforeUpload={()=> {
return false; }}
onChange={e => onChangeFile(e, index)}
onRemove={e => onRemoveFile(e, index)}
>
<Button
icon={<UploadOutlined />}
>
Upload a file
</Button>
</Upload>

After uploading the file, a remove icon appears. When I click on the remove button the file does not get removed from the state.


here is the onChange function :


const onChangeFile = (info, index) => {
console.log("onChange info = " + info);
const newForm = form;
newForm.inputs[index].value = info.file;
setForm({
...form,
from: newForm
});
console.log("onChange form = " + form);
};

I tried removing it using onRemove function like this:


const onRemoveFile = (info, index) => {
console.log("onRemove info = " + info);
const newForm = form;
newForm.inputs[index].value = null;
setForm({
...form,
from: newForm
});
console.log("onRemove form = " + form);
};

the output of the console logs :


enter


screenshot of the UI:


enter


feel free to try a few things in this code example provided by antd:


https://codesandbox.io/s/qj6n3?file=/index.js


More From » reactjs

 Answers
7

You can achieve that by following this example:


const normFile = (e) => {
if (Array.isArray(e)) {
return e;
}
return e && e.fileList;
};

<Form onFinish={() => {}}>
<Form.Item
name="tagList"
label="Upload"
valuePropName="list"
getValueFromEvent={normFile}
rules={[
{
required: true,
message: 'Tag list is required',
},
]}
>
<Upload
beforeUpload={() => false}
listType="picture-card"
>
<UploadOutlined style={{ marginRight: 5 }} />
Upload
</Upload>
</Form.Item>
</Form>


[#567] Saturday, December 11, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
Sat, Apr 27, 19, 00:00, 5 Years ago
;