I want to add a custom data attribute to the Material-UI Button Component like described here. In my case this would look like this:
<Button
inputProps={{ 'data-testid'='clickButton' }}
>
Click
</Button
This solution doesn't work because there is no inputProps for the Button Component. So how can I set a custom data attribute to the Material-UI Button Component?
Create an icon buttonImport the IconButton component from the Material-UI core package. import IconButton from '@material-ui/core/IconButton'; Render the icon as a child component to the IconButton . You can also move the color prop to the IconButton .
Use the target. dataset property to access data attributes from the event object in React. The dataset property provides read and write access to the custom data attributes of the element. The property returns a Map of strings which can be converted to an object.
You just add the prop directly to Button
:
<Button data-testid="clickButton">Click</Button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With