Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file with redux-form?

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?

like image 514
Mike Doudkin Avatar asked Sep 26 '16 08:09

Mike Doudkin


People also ask

Can I use redux form without redux?

1 Answer. Show activity on this post. Generally, it's expected that something with 'redux' in title won't work without Redux, otherwise this would be a terrible name for a package. If a package is advertised to be useable with Redux but isn't limited to it, its name doesn't contain 'redux'.

Is redux form good?

Redux-form is a really great library for working with validations. You can simply develop a lot of validations for different situations. Hence it provides validation functions to validate all the values in your form at once. You may also provide individual value validation functions for each Field or FieldArray.


1 Answers

Create a Field Component like:

import React, {Component} from 'react'  export default class FieldFileInput  extends Component{   constructor(props) {     super(props)     this.onChange = this.onChange.bind(this)   }    onChange(e) {     const { input: { onChange } } = this.props     onChange(e.target.files[0])   }    render(){     const { input: { value } } = this.props     const {input,label, required, meta, } = this.props  //whatever props you send to the component from redux-form Field     return(      <div><label>{label}</label>      <div>        <input         type='file'         accept='.jpg, .png, .jpeg'         onChange={this.onChange}        />      </div>      </div>     ) } } 

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.

like image 80
bh4r4th Avatar answered Sep 19 '22 23:09

bh4r4th