I have a functional component that returns an anchor element that can have the attributes href
and role
, depending on the props
that are passed to it:
function Item(props) {
return (
<a {...props.toSubMenu && {role: 'button'}} {...props.href && {href: props.href}}>
{props.children}
</a>
);
}
While creating this component, I first tried to pass props
without using the spread operator like so:
<a {props.toSubMenu && {role: 'button'}}>
, but this gave me the error:
Parsing error: Unexpected token, expected "..."
So at the moment, I have my component working the way I want it, but I cannot figure out why I must use the spread operator. In other parts of my code (like on line 4) I do not need it. I have read about it in this question and this one and in the react documentation, but I am still uncertain.