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

88
Views
React - 3 same inputs but only one change it state

I have a custom file input component, which I created to show the preloaded image when the person uploads their file.

But the problem is, I'm using this component three times, and independent of which input I choose to upload, only the first input is affected and re-renders. Anyone faced this? Should I use custom names for the variables of useState()?

The custom input:

const ImageInput = ({ name, register, registerName }: ImageInputProps) => {
    const [hasFileUploaded, setHasFileUploaded] = useState(false);
    const [currentFile, setCurrentFile] = useState(null);

    const handleImageChange = (e : React.FormEvent<HTMLInputElement>) => {
        setHasFileUploaded(true);
    }

    return (
        <div className="input_box">
            <input type="file" id="file" className="ImageInput" {...register(registerName)} onChange={(e) => handleImageChange(e)} />
            <label htmlFor="file" >
                { hasFileUploaded ? (
                    <>
                        <FontAwesomeIcon icon={faFighterJet} />
                        <p className="input_file_p">{name}</p>
                    </>
                ) : (
                    <>
                        <FontAwesomeIcon icon={faCamera} />
                        <p className="input_file_p">{name}</p>
                    </>
                ) }
                
            </label>
        </div>
    )
};

How I'm using it:

<div className="fc_center_images">
    <div>
        <ImageInput name="Frente CREA" register={register} registerName="cadastro-frente-crea" />
    </div>
    <div>
        <ImageInput name="Verso CREA" register={register} registerName="cadastro-verso-crea" />
    </div>
    <div>
        <ImageInput name="Selfie c/identidade" register={register} registerName="cadastro-selfie" />
    </div>
</div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

each element should have a unique id so I suggest to pass the id as well

const ImageInput = ({ name, register, registerName, id }: ImageInputProps) => {
    const [hasFileUploaded, setHasFileUploaded] = useState(false);
    const [currentFile, setCurrentFile] = useState(null);

    const handleImageChange = (e : React.FormEvent<HTMLInputElement>) => {
        setHasFileUploaded(true);
    }

    return (
        <div className="input_box">
            <input type="file" id={id} className="ImageInput" {...register(registerName)} onChange={(e) => handleImageChange(e)} />
            <label htmlFor={id} >
                { hasFileUploaded ? (
                    <>
                        <FontAwesomeIcon icon={faFighterJet} />
                        <p className="input_file_p">{name}</p>
                    </>
                ) : (
                    <>
                        <FontAwesomeIcon icon={faCamera} />
                        <p className="input_file_p">{name}</p>
                    </>
                ) }
                
            </label>
        </div>
    )
};

you can call it like this :

<div className="fc_center_images">
    <div>
       <ImageInput name="Frente CREA" register={register} registerName="cadastro-frente-crea" id="id1"/>
    </div>
    <div>
       <ImageInput name="Verso CREA" register={register} registerName="cadastro-verso-crea" id="id2" />
     </div>
    <div>
       <ImageInput name="Selfie c/identidade" register={register} registerName="cadastro-selfie" id="id3"/>
    </div>
</div>
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!