Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!