关于在 antd 里面 upload 中 onChange 的触发问题

2019-12-12 11:02:31 +08:00
 zzlit

这是官方阿里云上传的例子。

这是我代码中遇到的只监听一次上传状态的解答

我在imgChange方法中打印了fileList有值,onChangeundefinedfileList是文件上传的状态是肯定有值的,但是onChange要在下面做判断为什么会是未定义呢?

因为这个未定义所以进不到if判断中从而无法更新fileList就导致了在onChange方法中只监听到了上传的一次状态(status: "uploading"),之后的(status: "done")就监听不到了,请求各位大佬解惑!

另外也有点搞不懂为什么要从this.propsvalue作为上传的fileList绑定值,我在父组件中也没有定义这个参数啊?

附上我的相关代码:

class ShopUnitDialog extends Component {

  /*省略部分代码*/
  
  imgChange = ({fileList}) => {
    const { onChange } = this.props
    console.log('Aliyun OSS:', fileList, onChange) // fileList 有值,onChange 未定义
    if (onChange) {
      onChange([...fileList])
    }
  }

  render() {
    const { visible, value, ossData, onHide } = this.props
    const { Item } = Form
    const { getFieldDecorator } = this.props.form
    const upload = {
      name: 'file',
      listType: 'picture',
      fileList: value,
      action: ossData.host,
      data: this.getExtraData,
      beforeUpload: this.beforeUpload,
      onChange: this.imgChange,
    }
    return (
      <Modal
        title="详情"
        okText='确认修改'
        cancelText='取消'
        width='860px'
        visible={visible}
        onOk={this.submit}
        onCancel={onHide}
        destroyOnClose
      >
        <Form>
          <Item label='上传照片'>
            {getFieldDecorator('upload_img')(
              <Upload {...upload}>
                <div>
                  <img src={UploadImg} alt='上传' width='104' height='78' />
                </div>
              </Upload>
            )}
          </Item>
        </Form>
      </Modal>
    )
  }
}

const ShopUnitD = Form.create({})(ShopUnitDialog);
export default ShopUnitD

3105 次点击
所在节点    React
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/628269

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX