1

我想fetch在 filepond-react 组件中手动触发该方法。为了简化,我削减了其他道具。

  // FileUpload Wrapper
  <div className="file-upload">
    <FilePond
      ref={this.props.innerref}
      files={this.state.files}
      onupdatefiles={this.updateFiles}
      {...this.props}
    />
  </div>

  // Calling the wrapper in the parent component
  <FileUpload
    innerref={this.fileRef}
    server={{
      fetch: this.onFileFetch,
      process: this.onUpload,
      revert: this.onFileRevert,
    }}
  />

服务器方法

// fetch
onFileFetch = async (url, load, error, progress, abort, headers) => {
  // function triggers but cannot access load, progress etc. here
}

// upload
onUpload = async (fieldName, file, metadata, load, error, progress, abort) => {
  // function triggers and can access load, error etc.
}

// delete/revert
onFileRevert = async (uniqueFileId, load, error) => {
  // function triggers and can access load, error etc.
}

行为和触发

因此,当我分别单击上传和还原图标时,会触发上传和删除。我想fetch()手动触发,为此我打电话this.fileRef.current.props.server.fetch()

为什么不能访问该函数的参数?(加载、错误等)
我在下面添加了 filepond 对象this.fileRef.current以供参考。

```
{current: FilePond}
  current: FilePond
    isMounted: (...)
    replaceState: (...)
    props:
      ref: (...)
      files: []
      instantUpload: false
      oninit: undefined
      labelIdle: "Drag file(s) here<br> or <br><br><span class="filepond--label-action">Browse</span>"
      onupdatefiles: ƒ (fileItems)
      innerref: {current: FilePond}
      className: "in-block s3-bucket-file-input"
      server:
        fetch: ƒ (url, load, error, progress, abort, headers)
        load: ƒ (source, load, error, progress, abort, headers)
        process: ƒ (fieldName, file, metadata, load, error, progress, abort)
        revert: ƒ (uniqueFileId, load, error)
        restore: ƒ (uniqueFileId, load, error, progress, abort, headers)
        remove: ƒ (source, load, error)
        __proto__: Object
      get ref: ƒ ()
      __proto__: Object
    context: {}
    refs: {}
    updater: {isMounted: ƒ, enqueueSetState: ƒ, enqueueReplaceState: ƒ, enqueueForceUpdate: ƒ}
    allowFilesSync: true
    _reactInternalFiber: FiberNode {tag: 1, key: null, stateNode: FilePond, elementType: ƒ, type: ƒ, …}
    _reactInternalInstance: {_processChildContext: ƒ}
    state: null
    _element: input.in-block.s3-bucket-file-input
    _pond: {on: ƒ, onOnce: ƒ, off: ƒ, setOptions: ƒ, addFile: ƒ, …}
    addFile: ƒ addFile(source)
    addFiles: ƒ addFiles()
    getFile: ƒ getFile(query)
    processFile: ƒ processFile(query)
    prepareFile: ƒ prepareFile(query)
    removeFile: ƒ removeFile(query)
    moveFile: ƒ moveFile(query, index)
    getFiles: ƒ getFiles()
    processFiles: ƒ processFiles()
    removeFiles: ƒ removeFiles()
    prepareFiles: ƒ prepareFiles()
    sort: ƒ sort(compare)
    browse: ƒ browse()
    __proto__: Component
    __proto__: Object
```
4

1 回答 1

1

如果您手动调用this.fileRef.current.props.server.fetch(),则不会将任何参数传递给函数。FilePondurl, load, error, progress, abort, headers在调用函数时会传递这些参数,所以如果你想这样使用它,你也应该这样做。

这些功能不必标记,因为async它们可以是普通功能。

于 2020-01-07T08:02:34.043 回答