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

172
Views
How to dynamically check if input is empty without using onChange?

I want to get one-time information from the input control when value is empty and when value is not empty

Right now I'm using onChange like below:

<Input onChange={onChangeValue()} />

But this onChangeValue method is running every time when I type into the input, but I want two times information, first when value from input is empty and second when value from input is not empty.

Can someoone tell me what can I do this?

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

0

You can put some logic before your onChangeValue() function that checks the input value's length and then calls onChangeValue():

beforeOnChangeValue = function() {
  if(arguments[0].target.value.length <= 1) return onChangeValue(arguments)
}
<Input onChange={beforeOnChangeValue} />

This way the onChangeValue() function will only be called when the input is either emptied or when the first character is put in

about 4 years ago · Juan Pablo Isaza Report

0

it seems like you're using controlled components (changing the value of the control on every key typing). I think based on your strategy that you could use a flag in the onChange handler like this

  function onChange (evt) {
    setIsEmpty(!!evt.target.value);
    // Your code ...
  }

Then you'll always know if the input is empty or not.

   function Form () {
     const [isEmpty, setIsEmpty] = React.useState(false);
     
      // onChange here

      return <Input className={isEmpty ? 'empty-class' : 'non-empty-class'} />
   }
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!