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

451
Vistas
Multiple Typescript <Select> onChange handler

first let me say that I'm not a TS developer. So if I make a dumb question, let me know.
I'm trying to use multiple select, basically I need multiple dropdown menus.
Started with the simple select from material-ui and I tried to develop forward.

I make it work with a single select, my problem is with that I don't understand how to add a new select using the onChange event

type OnChangeValue = {
  locationNaming: unknown;
};

interface ServerNamingDropdownProps {
  onChange(value: OnChangeValue): void;
  error?: boolean | undefined;
  initialValue?: string | undefined;
}

export const ServerNamingDropdown: React.FC<ServerNamingDropdownProps> = ({
  onChange,
  error,
  initialValue = undefined,
}) => {

  const [locationNamingSelected, setLocationNaming] = useState(
    initialValue ? initialValue : '',
  );

  return (
    <div>
      <FormControl sx={{ minWidth: 120 }}>
        <InputLabel id="demo-simple-select-label">Location</InputLabel>
        <Select
          labelId="demo-simple-select-label"
          id="demo-simple-select"
          value={locationNamingSelected || ''}
          label="Location"
          error={error}
          labelWidth={140}
          style={{ width: 300 }}
          onChange={e => {
            onChange({
              locationNaming: e.target.value,
            });
            setLocationNaming(e.target.value as string);
          }}        >
          {location.map(item => (
            <MenuItem value={item.abbreviation} key={item.key}>
              {item.name}
            </MenuItem>
          ))}
        </Select>
    </div>
  );
};

I have to add a new dropdown for Department.
I understand that should add to OnChangeValue the departmentNaming.
But I don't understand how to manage the onChange.

Any idea would be greatly appreciated.

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

React will re-run your render when you change your state, i.e. when you call setLocationNaming. The output of the render function should be different at that point. As @mstephen19 said, what your function returns can be conditional on the props or state. Here's a very simple (untested) sketch:

const MyConditionalRender = () => {
  const [showMoreContent, setShowMoreContent] = useState(false);

  return (
    <div>
      <button onClick={() => setShowMoreContent(true)}>Show</button>
      {showMoreContent && (
        <p>More Content, like an extra dropdown for the department.</p>
      )}
    </div>
  );
};

Do read the Conditional Rendering docs that mstephen19 linked.

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