Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Value is changed by setState, but React form submits the ORIGINAL value

I have an issue with a modified field value not "sticking" to the form upon submission.

I'm creating a form where users can upload an image. After the image is uploaded, the hosting service returns the image URL, which is saved into a state variable with

this.setState({
            imageUrl: info.secure_url
})

In the TextField, I have the value set as

<TextField name='imageUrl' value={this.state.imageUrl}/>

This works fine and shows up on the form, but an issue arises when the form is submitted. The form does not recognize that there is something in that field, and throws an error stating input is required.

The submit function works fine with user entered fields and successfully submits data to the database.

Here is the full code

class SubmitVaccine extends React.Component {
  ...
  state = {
    imageUrl: null,
    imageAlt: null,
  }
  uploadImage = () => {
        ...
        (code to upload image here)
        ...
        if (event === 'success') {
          this.setState({
            imageUrl: info.secure_url
          }
  };

  render() {
    const { imageUrl } = this.state;
    return (
      <Grid>
                ...
                (code to upload image here)
                ...

                <TextField name='imageUrl' value={this.state.imageUrl}/>
                <SubmitField value='Submit' id="btn-custom"/>
                ...
      </Grid>
    );
  }
}

Here are some visuals. Default this.state.imageUrl is set to null, so field shows up empty

enter image description here

After image is uploaded, the image URL shows up because this.state.imageUrl has changed

enter image description here

However, the text field is required and it errors out when the field is empty upon submission. This is exactly what is happening here, but the field is filled! Where should I look for clues on how to solve this? enter image description here

UPDATE

UPDATE

UPDATE

I found out the form errors out because it submits the original, NOT updated state of imageUrl, which was set to null. null = empty field = error upon submission.

However, we can visually see that the field shows an updated imageUrl. How do I make the form submit the new imageUrl?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you didn't have a onChange function on your TextField component, you should make an onChange function to update your state whenever you type into it, like so

handleChangeURL(event) {
    this.setState({imageURL: event.target.value});
}

and put it on

<TextField name='imageUrl' value={this.state.imageUrl} onChange={this.handleChangeURL}/>

Just make sure that your TextField component calls the onChange function properly

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!