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

108
Views
Add new text field upon selecting a value using immutability helper in reactjs

I have a list of options

const values = [
      { firstName: 'John',
        id: 'text'
      }.
      {
        userId: 123,
        id: 'number'
      }
];

The logic is that the user selects one of the values (firstName) and an input field will be generated of the type 'string'. If the user selects userId, then a text input will be generated of the type 'int'. I need to create a logic where the user will be able to select another option from the drop-down and a text field should be generated. For example, if the option selected is True or another number and it should provide a boolean or an integer text field. I am also using immutability helper to update the type, but I did not clearly understand much from the official docs. Any idea on how to implement this with react?

Here is my immutability helper function

const setValueType = (type: string, index: number) => {
    if (index < 0 || index >= data.values.length) {
        return;
    }
    setValueData(update(data, {
        values: {
            [index]: {
                type: { $set: type }
            }
        }
    }));
};
const setPlaceholder = (placeHolder: any, index: number) => {
    if (index < 0 || index >= data.values.length) {
        return;
    }
    setValueData(update(data, {
        values: {
            [index]: {
                placeHolder: { $set: placeHolder }
            }
        }
    }));
}

I am using 'setValueType' function to update the type, and 'setPlaceHolder' to set a display text, I have to come up with a way where I will be adding new options to the drop-down, but they should render conditionally based on the type.

I am thinking that I have to pass an if-else condition in the 'setValueType' function, but I am not sure on implementing it.

about 4 years ago · Juan Pablo Isaza
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!