J
J
Jedi2020-11-26 00:59:11
React
Jedi, 2020-11-26 00:59:11

How to add class via rest?

function Button({children, ...rest}) {
    return (
        <button className="great-button">
            {children}
        </button>
    );
}


If you try like this, it's logical that it won't work.

function Button({children, ...rest}) {
    return (
        <button {...rest} className="great-button">
            {children}
        </button>
    );
}


How can I correctly add a class to buttonwithout losing the existing one .great-button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-11-26
@PHPjedi

const Button = ({ children, className = '', ...rest }) => (
  <button {...rest} className={`button ${className}`}>
    {children}
  </button>
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question