DEV Community

Cover image for Embedding Data Into React/JSX Elements

Embedding Data Into React/JSX Elements

Adam Nathaniel Davis on March 22, 2023

What I'm about to show you is really pretty basic. So Code Gurus out there can feel free to breeze on by this article. But I've rarely seen this ...
Collapse
 
starkraving profile image
Mike Ritchie

This approach can be improved by using data attributes in the target element handled by the click event. For example

const handleClick = (event) => {
  const paintIndex = event.target.dataset.paintindex;
  // do something with the value
}
<td data-paintindex={paintIndex} onClick={handleClick}>{paintIndex}</td>
Enter fullscreen mode Exit fullscreen mode

Data attributes can be applied to any element and are guaranteed never to interfere with native functionality provided by native attributes. Note that child tags on the element will mean that you may have to traverse up the parent tree back to the intended target before you can access the attributes. But it’s nice because you can have as many attributes as you need without trying to figure out what native attribute to use.

Great article!

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

Great point! And yeah, I should've touched on data attributes as well. Thank you!

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Just did a quick cross-read (bookmarked for later fully-comprehensive reading) and I'm wondering if Styled-Components wouldn't apply better to this use-case (so you just pass the color as prop and you don't need to generate all classNames beforehand/through a process) 😁

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

That's basically what I covered under the "Wrapper Components" section.

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Amazing!
See, that's why I need to read it calmly afterwards πŸ˜‚πŸ˜‚ cross-reading is never enough πŸ˜…

Thread Thread
 
bytebodger profile image
Adam Nathaniel Davis

LOL. To be fair, I didn't specifically put it in the context of styled components. But it's basically the same concept.

Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡

haha yes, I've seen it briefly 😁 ty!

Thread Thread
 
400shynimol profile image
shynimol 400

Just did a quick cross-read (bookmarked for later fully-comprehensive reading) and I'm wondering if Styled-Components wouldn't apply better to this use-case (so you just pass the color as prop and you don't need to generate all classNames beforehand/through a process) 😁

Thread Thread
 
bytebodger profile image
Adam Nathaniel Davis

There's a whole section on "Wrapper Components"...

Collapse
 
krlz profile image
krlz

Wow, this article is awesome! The way you explain the technique for extracting bits of data from JSX elements is on point. I'm pumped to see more content like this from you in the future! Keep up the great work!

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

Thank you!

Collapse
 
mstewartgallus profile image
Molly Stewart-Gallus • Edited

You could also use data as well as data attributes. I mostly use data attributes for CSS.

For this specific case it's also possible to use useMemo.

const TableCells = ({cells}) => useMemo(() => cells.map(cell => {
      const paintIndex = colors.findIndex(color => color.name === cell.name);
      return {
        paintIndex,
        onClick(e) {
          return handleCellClick(paintIndex);
      };
}), [cells])
.map(({ paintIndex, onClick }) =>
<td onClick={onClick)>
  {paintIndex}
</td>
);
~~~
Enter fullscreen mode Exit fullscreen mode

Some comments have been hidden by the post's author - find out more