Do like below to add your custom attributes to a component
Expected Parent Component :
class App extends Component {
constructor() {
super();
this.state = {
name: 'React'
};
}
render() {
let selDynamicAttributes = {"data-attr":"someString", "MyCustomAttribute":"foo"}; // see here added custom attrib
return (
<div>
<Button {...selDynamicAttributes} /> //passed custom attributes
</div>
);
}
}
Expected Child Component:
export default class Button extends React.Component {
constructor(props) {
super(props);
}
render(){
const { ...otherAttributes } = this.props;
return(
<div>
<button
{...otherAttributes}>
{this.props.displayText}
</button>
</div>
);
}
}
Working Demo
Read more at : How to add custom html attributes in JSX