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

143
Vistas
How to set HTML attributes with variables?

In my HTML I have a lot of repeated elements with quite a few attributes each and most of them are the same. It's taking up a lot of space and I could clean up the code if I could just set the attributes as a variable and use that variable in all my elements. So is there a way to do this?

For example one of my elements look like this,

<button className='btn btn-secondary dropdown-toggle' style={{ margin: "5px" }} type='button' data-bs-toggle='dropdown' aria-expanded='false'>
  Delete
</button>

And there are at least 10 other buttons with the exact same set of attributes and takes up almost 100 lines worth of space. Is there a way I can set the attributes in one place then apply it to every element to save space?

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

0

If you are reusing a collection of html attributes, here are two ways you could handle this:

  1. Save the attributes into an object and spread the object onto each <button> that uses them.
  2. Create a <CustomButton> component and encapsulate the shared attributes in the component. Because each attribute is exposed as a prop in the component interface, each prop/attribute has a default value but can be overwritten if necessary.

// #1
const defaultButtonProps = {
  className: "btn btn-secondary dropdown-toggle",
  style: { margin: "5px" },
  type: "button",
  "data-bs-toggle": "dropdown",
  "aria-expanded": "false"
};

export default function App() {
  return (
    <div className="App">
      <div>
        {/* #1 */}
        <button {...defaultButtonProps}>Delete</button>
        <button {...defaultButtonProps}>Save</button>
      </div>
      <div>
        {/* #2 */}
        <CustomButton />
        <CustomButton>Save</CustomButton>
      </div>
    </div>
  );
}

// #2
const CustomButton = ({
  className = "btn btn-secondary dropdown-toggle",
  style = { margin: "5px" },
  type = "button",
  dataBsToggle = "dropdown",
  ariaExpanded = "false",
  children = "Delete"
}) => {
  return (
    <button
      className={className}
      style={style}
      type={type}
      data-bs-toggle={dataBsToggle}
      aria-expanded={ariaExpanded}
    >
      {children}
    </button>
  );
};

Edit kind-ellis-0kejip

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