react-redux - 异步验证后的 Redux-Form 文件上传

标签 react-redux redux-form react-redux-form

我有一个通过 redux-form 字段连接的 FileUpload 组件。当在输入字段中选择文件时,它会调用 input.onChange 和 input.onBlur 并将所选文件作为 base64 字符串。

我正在使用 asyncValidator redux-form 选项来验证图像的尺寸,并且我希望在异步验证完成后将文件上传到我的服务器。

似乎没有记录任何类型的 afterAsyncValidation Hook 。以 redux 形式完成此任务的最佳方法是什么?

最佳答案

redux-form设计的想法是您的数据将在提交时保存到服务器。

然而,没有什么能阻止你把自己的 .then()异步验证之后的子句来做到这一点。像这样的东西?

// async function you already have that is looking at your
// picture field and rejecting the promise with errors
import validateDimensions from './validateDimensions'

// async function to upload the image
import upload from './uploadImage'

const MyForm = reduxForm({
  form: 'myForm',
  asyncValidate: values =>
    validateDimensions()
      .then(() => {
        // we know validation passed
        upload(values.prettyPicture)
        // ^ not "return upload(values.prettyPicture)" to let this async
        // validation promise resolve and do upload asynchronously
        // after it has resolved
      })
  },
  asyncBlurFields: [ 'prettyPicture' ]
})(MyForm)

关于react-redux - 异步验证后的 Redux-Form 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41190277/

相关文章:

javascript - 如何使用 redux-form 以编程方式更新字段的状态

reactjs - 在react-redux-form中,如何禁用提交按钮,直到所有必填字段都未填写

reactjs - Redux saga测试select效果

javascript - 您可以在 onClick 函数中使用 useState 钩子(Hook)吗?

reactjs - 根据 redux 表单中的另一个字段验证一个字段

reactjs - redux-form,根据其他字段值设置字段可见性

javascript - react : The `value` prop supplied to <select> must be a scalar value if `multiple` is false

javascript - React-Redux 更改值的状态不会反射(reflect)在更改上

javascript - 使用 redux saga 在 React Native 中实现 token 刷新

javascript - 如何在 onSubmit 调用的函数内使用状态?