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

103
Views
Comparing two values from Dropdown

I have a Dropdown from which I pass the Name value OnClick

onClick={() => clicked(v.Name)

Then, in the clicked event I want to compare that the input value is not the same as old value but this fails because I cannot update oldName after comparing the values. Probably there is a better logic to this. I also tried UseEffect and UseState but they tended to update with previous values.

function DropdownComponent() {

    var oldName;

    function clicked(Name) {
        if (Name === oldName) {
            console.log("Same Clicked!");
        }
        else {
            console.log("Differet Clicked!");
            setTitleName(Name)
        } return oldName = (Name); //can't update
    }

    return (
    <Dropdown //dropdown component

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

0

Anything stored in a component-local variable only exists for that render. Each render will re-declare the variable. So this variable:

var oldName;

Will always be undefined on every render. Component-local variables can be useful for temporary storage. For example, you can perform sorting/filtering logic on an array and store the results in variables for the purpose of mapping those variable to UI components in the render, effectively separate the sorting/filtering logic from the rendering logic.

But those variable do not persist across renders.

If you want something to be retained across renders, you want to keep it in state. Which you appear to already be doing here:

setTitleName(Name)

The code shown doesn't include this, but presumably somewhere you're creating a state value like this?:

const [titleName, setTitleName] = useState('');

If we are to assume that this "title name" is a state value then you already have the "previous value", it's the one you're keeping in state. So just check against that one:

function clicked(name) {
    if (name === titleName) {
        console.log("Same Clicked!");
    } else {
        console.log("Differet Clicked!");
        setTitleName(name);
    }
}

Live demo

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!