Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda