Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a data attribute to Material-UI Button?

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?

like image 978
Niklas Weber Avatar asked Nov 02 '20 17:11

Niklas Weber


People also ask

How do I add icons to material UI button?

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 .

How do you access data attribute in React?

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.


1 Answers

You just add the prop directly to Button:

<Button data-testid="clickButton">Click</Button>

Edit Button data attribute

like image 77
Ryan Cogswell Avatar answered Oct 22 '22 10:10

Ryan Cogswell