0

I have a Field Component (redux-form) that calls a custom ImageInput component that uploads an image, then spits out the src.

I need to pass that src from my child component, to the parent where it updates the parents state via a handler.

I've tried to follow this answer, but am a bit confused as my setup seems a little different: How to update parent's state in React?

Here is my parent component

handleChange(e) {
  console.log(e)
  this.setState({[e.target.name]: e.target.value});
}

<Field
  {...this.props}
  component={ImageInput}
  name="templating.img"
  onChange={this.handleChange.bind(this)}
/>

and then my child where the image is inputted and uploaded

constructor(props) {
  console.log('props', props);
  super(props);
  this.state = {
    src: null
  }
  console.log(this.state)
}

_imgUpload = (e) => {
  e.preventDefault();
  // console.log(e.target.files)
  if (e.target.files.length === 1) {
    let file = e.target.files[0]
    loadImage(e.target.files[0])
    .then(uploadThumbor)
    .then(formatImage)
    .then(({src, dataUri}) => {
      this.setState({src: src})
      console.log('img', src)
    })
  }
}

/* Snippet where the image upload occurs */
<div style={styles.image}>
  <div style={styles.defaultPreview}></div>
  <div style={styles.sample}></div>
  <input type="file" style={styles.uploadPreview} accept="image/*" onChange={this._imgUpload} />
</div>

After an image has been upload, I am setting the state in the child component. I need to pass that state to the parent where it'll update it's state. I dont think the onChange={this.handleChange.bind(this)} is correct for this parent component in this instance (it is correct for other Field components that are simple inputs).

Any help would be awesome.

jobiin
  • 449
  • 2
  • 5
  • 12

1 Answers1

0

I get what you are trying to do. You said that you are using redux form. Is this the only input in the form? Why is it uploading the image separately as soon as it changes?

I ask this cause the call to the api is normally dispatched form the parent container. If this api call is specific to this page I would suggest moving it to the parent and maintain the src state in the parent. If you need it here you can always pass it down as prop.

If this is not an option you can always have a separate callback function similar to the handleOnChange. It would be called when you get the new src from the backend. This will allow you to maintain the src in the parent state as you wanted. In this case, if you need to have the src in the input component you can do the same as in the other solution, pass it down as a prop.

Parent

handleChange(e) {
  console.log(e)
  this.setState({[e.target.name]: e.target.value});
}

handleSrcChange(src) {
  this.setState({ imageSrc: src });
}

<Field
  {...this.props}
  component={ImageInput}
  name="templating.img"
  onChange={this.handleChange.bind(this)}
  onSrcChange={this.handleSrcChange.bind(this)}
/>

Input Component

_imgUpload = (e) => {
  e.preventDefault();
  // console.log(e.target.files)
  if (e.target.files.length === 1) {
    let file = e.target.files[0]
    loadImage(e.target.files[0])
    .then(uploadThumbor)
    .then(formatImage)
    .then(({src, dataUri}) => {
      this.props.onSrcChange(src);
      console.log('img', src)
    })
  }
}

/* Snippet where the image upload occurs */
<div style={styles.image}>
  <div style={styles.defaultPreview}></div>
  <div style={styles.sample}></div>
  <input type="file" style={styles.uploadPreview} accept="image/*" onChange={this._imgUpload} />
</div>