张延森 5 years ago
parent
commit
20e36c6a73
1 changed files with 16 additions and 7 deletions
  1. 16
    7
      src/components/uploadImage/ImageUpload.jsx

+ 16
- 7
src/components/uploadImage/ImageUpload.jsx View File

11
     loading: false,
11
     loading: false,
12
     previewVisible: false,
12
     previewVisible: false,
13
     previewImage: '',
13
     previewImage: '',
14
-    fileList: [],
14
+    fileList: this.getPropsFileList(),
15
   };
15
   };
16
 
16
 
17
-  componentDidUpdate() {  this.setState({ fileList: [{
18
-    uid: '-1',
19
-    name: 'image.png',
20
-    status: 'done',
21
-    url: this.props.value,
22
-  }] })  }
17
+  componentDidUpdate(prevProps) {
18
+    // 直接 setState 会死循环
19
+    if (prevProps.value !== this.props.value) {
20
+      this.setState({ fileList: this.getPropsFileList() })
21
+    }
22
+  }
23
+
24
+  getPropsFileList() {
25
+    return !this.props.value ? [] : [{
26
+      uid: '-1',
27
+      name: 'image.png',
28
+      status: 'done',
29
+      url: this.props.value,
30
+    }]
31
+  }
23
 
32
 
24
 
33
 
25
   handleChange = ({ file, fileList }) => {
34
   handleChange = ({ file, fileList }) => {