如何使用 Redux Form 将 url 作为数据插入?

How to get the url to insert as a data with Redux Form?

我能够在附件字段中获得 url 但对于 redux 表单它是空的,如何将 url 的值传递给 redux 表单?下面是代码和截图:

<div className="FileUpload">
   <Dropzone
     onDrop={this.onImageDrop.bind(this)}
     multiple={false}
     accept="image/*">
<div>Drop an image or click to select a file to upload.</div>
   </Dropzone>
</div>
   <div className="form-group">
     <label htmlFor="attachment">Attachment:</label><br />
       <input className="form-control" focus placeholder="attachment" type="text" name="attachment" ref="attachment" value={this.state.uploadedFileCloudinaryUrl} />
        {this.state.uploadedFileCloudinaryUrl === '' ? null :
        <div>
          <p>{this.state.uploadedFile.name}</p>
            <img src={this.state.uploadedFileCloudinaryUrl} alt="" />            
        </div>}
        </div>
        <div className="ui small image">
          <img src={this.props.workrequest.attachment} alt="" />
        </div>

the url in the attachemnt field

第一个是使用 React Dropzone 获取 url 但对于 Redux Form 它是空的。我可以知道如何在此处的 Redux Form 中插入 url 吗?谢谢

其实,你必须使用Redux-form的Field组件。 否则,您可以使用 dispatch 和 change action creator 更改表单值。

import { Field, change } from 'redux-form';

this.props.dispatch(change('uploadForm', 'url', this.state.url));

import React from 'React';
import { connect } from 'react-redux';
import { change, reduxForm } from 'redux-form';
import Dropzone from 'react-dropzone';

class UploadForm extends React.Component {
  onDrop = (accepted) => {
    if (!accepted.length) return;

    // start uploading
    this.setState({ isUploading: true });

    const formData = new FormData();
    formData.append('file', accepted[0]);

    axios.post('upload', formData).then(
      (res) => {
        this.props.dispatch(change('uploadForm', 'url', res.url));
        this.setState({ isUploading: false });
      },
      () => {
        this.setState({ isUploading: false });
      }
    );
  };

  render() {
    return (
      <form>
        <Dropzone onDrop={this.onDrop} />
      </form>
    );
  }
}

export default connect()(
  reduxForm({
    form: 'uploadForm',
    initialValues: {
      url: ''
    }
  })(UploadForm)
);

请使用这个。