I have some duplicating code across my project that looks like this:
function MyComp({prop1, prop2, ...restProps}) {
// ...
const dataAttributes = Object.keys(restProps).reduce((acc, key) => {
if (key.startsWith('data-')) {
acc[key] = restProps[key];
}
return acc;
}, {});
return (
<div {...dataAttributes}>
{/* ... */}
</div>
);
}
I'm thinking about automation of this behaviour (pass props that start with data- to the root element of the component if it's an html element).
Any thoughts?