How to add additional props to a React element passed in as a prop?
Pass in the component constructor instead of an instance:
class Menu extends React.Component {
render() {
return(
<div className="Menu">
<MenuItem icon={MdInbox} />
<MenuItem icon={MdDrafts} />
<MenuItem icon={MdTrash} />
</div>
);
}
}
The child class:
class MenuItem extends React.Component {
render() {
// This constant must begin with a capital,
// it’s how React distinguishes HTML elements from components.
const Icon = this.props.icon;
return(
<div className="MenuItem">
<Icon size={24} />
</div>
);
}
}
You can set icon size with React.cloneElement
API
class MenuItem Extends React.Component {
render() {
return(
<div className="MenuItem">
{React.cloneElement(this.props.icon, { size: 24 })}
</div>
);
}
}