I have a question about React Syntax.
I was conceptualising a rebuild of my website in React and was writing code to access the data-attribute
value.
The method I used to get the data-attribute
value was:e.target.getAttribute('data-menuItem');
and that seemed to work just fine. Upon further investigation I read about the alternative notation for the same method looks like:e.target.attributes.getNamedItem('data-menuItem').value
I would just like to know if the second method I mentioned is best practice or if it really matters at all.
Your help is much appreciated.
Thanks
Moe
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.
Pass Dynamic Values to an Attribute The example below demonstrates this. Create one state variable with a default value. Now create the <input> element along with the required attributes. In the above example, the game-changer props are onChange() , used to update the existing value with the updated value.
react-virtualized was specifically designed for rendering large lists and tabular data. It uses a technique similar to infinite scroll called windowing that renders only the visible parts of a list to the screen.
There are basically two ways in which the data gets handled in React. It gets handled through state and props. In another way, it can be said that two types of Model in React are there to manage the data for the components. Both state and props are plain JavaScript objects.
Let suppose that you have <div data-pg="abc"></div>
in your html then in react you can retrieve data attributes:
let val = e.target.dataset.pg
Now your val
will have abc
.
To retrieve value of data attribute, alternative way is:
let val = e.target.getAttribute('data-pg')
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