Child Component:
export default class Button extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<div className="form-group">
<button
// Need to add dynamic html attr here e.x: data-id
key={index}
id={id}
className={`btn btn-default ${componentClass ? componentClass : null }`}
type="button"
onClick={this.props.onClick}>
{text}
</button>
</div>
);}}
Parent Component :
import Button from './Button';
Class App extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div className="s">
<Button data-id="exampleData" /> // Need to add data-id attr to child button
</div>
);
}
Button Component, have it's own default attributes like mentioned above : id,className,type,onClick
Parent Component, will call Button component and add some additional attributes like data-id,onChange.
note : after searched few ideas, I know that i can use spread operators like below :
Parent Component :
let dynamicAttributes = {"data-id":"someString", "data-attr":"someString", "data-url":"someString"};
return (
<div className="s">
<Button dataSrc={ btnSrc } {...dynamicAttributes} />
</div>
);
I don't know how to call the dynamicAttributes in Button component as a html attrib
Expecting a good solution to this. Thanks in advance.
Used Destructing and Babel Showing error(unexpected token) like below image.
note: Already installed preset-env and preset-react.