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

264
Vistas
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 Respuestas
Responde la pregunta

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 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