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

259
Visualizações
className in destructured prop doesn't apply to component sometimes, is it a bug? (Next.JS)

I have a component as such:

import classNames from "classnames";

const Button = (props) => {
  const { className, children, ...rest } = props;
  const [internalProps, setInternalProps] = useState(rest);
  const [classes, setClasses] = useState("");

  const validateProps = () => {
    const tmp: Props = Object.assign(rest, {});
    const classes = [
      "btn",
      className,
      { "btn-block": block },
      { "btn-sm": size === "sm" },
      { "btn-lg": size === "lg" },
    ];

    setClasses(classNames(classes));
    setInternalProps(tmp);
  };

  useEffect(validateProps, [props]);

  return (
    <button className={classes} {...internalProps}>
      {children}
    </button>
  );
};

The className prop is taken out of internalProps because for whatever reason, when included in it, the classes aren't applied sometimes when the page is loaded. I have no idea why this is happening, AFAIK className is treated as just another prop in React. I am still new to React and Next.JS though, so I may be missing something basic here. Has anyone experienced something like this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are setting internalProps to be the same as rest, and rest doesn't include className.

This line:

const { className, children, ...rest } = props

means:
From the props take className and children, and put all the remaining properties (not including className and children) into rest.

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