Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda