I can't get correct value into the store when trying to upload a file. Instead of file content, I get something like { 0: {} }
.
Here's the code:
const renderInput = field => (
<div>
<input {...field.input} type={field.type}/>
{
field.meta.touched &&
field.meta.error &&
<span className={styles.error}>{field.meta.error}</span>
}
</div>
);
render() {
...
<form className={styles.form} onSubmit={handleSubmit(submit)}>
<div className={styles.interface}>
<label>userpic</label>
<Field
name="userpic"
component={renderInput}
type="file"
/>
</div>
<div>
<button type="submit" disabled={submitting}>Submit</button>
<div>
</form>
...
}
All the examples on the web that I found were made using v5 of redux-form.
How do I do file input in redux-form v6?
I managed to do it with redux-form on material-ui wrapping TextField like this:
B4 edit:
After edit:
with component defined as:
If you need base64 encoding to send it to your backend, here is a modified version that worked for me:
Then your field component would look like:
<Field component={FileInput} name="primary_image" type="file" />
Another way to do it that will render a preview image (the below example uses React 16+ syntax and only accepts a single image file to send to an API; however, with some minor tweaks, it can also scale to multiple images and other fields inputs):
Working example: https://codesandbox.io/s/m58q8l054x
Working example (outdated): https://codesandbox.io/s/8kywn8q9xl
Before:
After:
containers/UploadForm.js
components/dropzoneField.js
components/imagePreview.js
components/placeholder.js
components/showError.js
styles.css
Create a Field Component like:
Pass this component to the Field component where you needed. No need of additional Dropzone or other libraries if you are after a simple file upload functionality.
My example of redux form input wrapper with Dropzone
Hot to use it: