DEV Community

Cover image for 99% of frontend devs don't use this

99% of frontend devs don't use this

Antonio Moruno Gracia on July 14, 2025

In the React world, most developers don’t think twice before using closures in their component render methods, especially when mapping over lists. ...
Collapse
 
quozzo profile image
Quozzo

Im not a fan of this approach because only serialisable data can be inserted into the DOM and then extracted back out into JS, and while you can simply use the ID to find the item in the array but doing so will cause so many loops that you lose any performance gained by removing the closures.

While always, it depends on the situation, so it's nice to have it in your pocket should the situation arise.

Collapse
 
js2me profile image
Sergey S. Volkov • Edited

new function references can cause unwanted re-renders

That's why functional components in React are sucks.

Also most React developers do not even know that useMemo useEffect useState and all other React hooks it is not only "magic hook" - this is functions which do some calculations inside.

Take a look at this great article

mbrizic.com/blog/react-is-insane/

I personally try to get rid of using React hooks, I'm using MVVM and MobX which works with classes

Collapse
 
js2me profile image
Sergey S. Volkov

Also if we will use data-* attributes then this is killing any types in TypeScript, so this is not good.

Collapse
 
js2me profile image
Sergey S. Volkov • Edited

Also modifying data-* attributes is not performant (calls reflow after change)

Collapse
 
bigcheeseh profile image
bigcheeseh

Every class component should have render function with all drawbacks of function components and without ability to use hooks but only terrible on componentShouldRecieveProps, already ReceivedProps, maybeItsHereTimeToHandlePropsButMaybeNot, componentWillUnmount etc. Do not remember exact names but it was very close to it

Collapse
 
js2me profile image
Sergey S. Volkov

Yes, agreed with you, but in hook way you must keep in mind how works useEffect with dependencies and how works closures in use* hooks. How simple works functional component and why declaration a lot of hooks in functional component body can be hurtful for performance.

Collapse
 
michael_1c56c1490fd191f72 profile image
Michael

This is a terrible article lol

Collapse
 
cmacu profile image
Stasi Vladimirov

Hard disagree. This article is extremely accurate representation of what 90% of enterprise and startup codebases contain right now. Anyone who denies it either has no experience or is too far the react cult to realize what’s happening.

Thread Thread
 
spock123 profile image
Lars Rye Jeppesen

So much this

Collapse
 
js2me profile image
Sergey S. Volkov

Why ?

Thread Thread
 
michael_1c56c1490fd191f72 profile image
Michael

React has plenty of issues, but he’s criticising React for problems he created himself because he doesn’t understand it and write shit code

Thread Thread
 
moruno21 profile image
Antonio Moruno Gracia

I’m not criticizing React. Actually I love it. I’m just presenting another way of doing things 😙

Thread Thread
 
spock123 profile image
Lars Rye Jeppesen

You should. at this moment, frameworks like Vue or Angular are now better and more modern / performant than React.

Collapse
 
geregur profile image
GEREGUR

youtu.be/DLa2FQQzCr4?si=rSh0tlV-R5...

tldr: this is not a great article

Collapse
 
cmacu profile image
Stasi Vladimirov

That video demonstrates and perfectly proves the points in the article by showcasing snippets from his own code that contain useEffect hooks and also sharing examples from large projects that contain similar problems…

TLDR: that video is an amazing demonstration why the article is accurate and how blind React worshippers are for their own misery.

Collapse
 
chinmaymoghe profile image
Chinmay

I am inclined to comment, just use Svelte and be happy 😁

Collapse
 
aaron1 profile image
Aaron

It's also not typesafe, and for large data or long lists you will be serialising the data into strings ahead of time, before you even need it. It's cheaper to make a new anonymous function than serialising large data sets to string.
Keeping the data in memory and only accessing when needed is sometimes more preferable.

Collapse
 
bernardigiri profile image
Bernard Igiri

With the rise of SSR this may become more important, however I remember the time when getting data out of the DOM was a big push. The thing is, you always want a single source of truth for your data. If you put things in the DOM and have things in memory, you will have to figure which one to trust when they fall out of sync. Moving everything to JavaScript is easy, because AJAX goes straight to JS. The DOM cannot query the server and only understands strings. So if the DOM is your source of truth, how are you going to get data into and out of it? Will it be able to keep up?

Collapse
 
paulvz_ao profile image
Paul Von Zeuner • Edited

I like this, I can see the uses for it.
But like everything in writing code, there are many ways to skin a cat.
And as engineers, we need to be aware of the security risks and use this with caution.

OK for UI-related non-sensitive data
Not OK for anything security-sensitive (roles, tokens, permissions)

Safe Use Example:
<div data-product-id={product.id} onClick={handleClick}>{product.name}</div>
function handleClick(e) {
const id = e.currentTarget.dataset.productId;
// Use it to fetch more info securely from backend
}

Unsafe Example:
<div data-auth-token={user.token}>...</div> // Don't leak sensitive data
<div data-role="admin" onClick={showAdminPanel}> // Role-based access is not secure on frontend

Collapse
 
zachbryant profile image
Zach • Edited

you're creating a new function for each item on every render

This isn't quite true. A new function is created each render, but this is the only function that gets called for every item map goes over, not per ID.

Collapse
 
poetro profile image
Peter Galiba

For buttons, checkboxes, radio buttons you could also use the name and value attributes.

Collapse
 
parag_nandy_roy profile image
Parag Nandy Roy

Such a simple trick but so underrated..

Collapse
 
b_maj_6cfdf9e36c5a12cf188 profile image
B Maj

I couldn’t get past the hyperbolic, made up statistic “99% of devs don’t do…” 😆

Collapse
 
baden1989 profile image
Baden

I've been using data-* with Stimulus controllers for a while now
I've never used react or know too much JS tho to be honest.

Collapse
 
yaireo profile image
Yair Even Or

negatable on performance and isn't safe. only applied to primitives. DO NOT USE THIS.

Collapse
 
rmohitjoe profile image
R. Mohit joe

i have been using this method for so long that means i am in that 1% .

Collapse
 
rajesh_patel_68e5dd6c9a4f profile image
Rajesh Patel

Absolutely loved this perspective 👏 — never thought of using data-* attributes this way in React. It’s such a clean and elegant alternative when optimizing large lists or working with React.memo.

Collapse
 
abhiwantechnology profile image
Abhiwan Technology

Yeah it is right, but nowadays most of the India based top 3d game development services provider companies were using new coding style to optimize the code as well as they were improving the coding technique by following latest coding trends.

Collapse
 
framemuse profile image
Valery Zinchenko • Edited

Aha, so we're falling back to VanilaJs because it's better... then maybe React itself is a problem?

Yeah, I know we all know that.
I just wanted to mention that I figured out a better alternative to React - denshya proton.

Yep, I know changing tools are difficult due to Market Jobs being oriented on React, but questioning status-quo is always good imho.

Collapse
 
myjsalterego profile image
Aleksey Litvinov • Edited

The approach to use data attributes is interesting. However, using performance as an argument does not look convincing without proper evidence. In a theory you can safe "some", but in practice it may be not a big deal.

Another alternative to avoid issues with memoized components and lists can be to implement list element as a separate component (with memo wrap) that will return desired value (passed as a prop) through one of the onClick arguments (same way you did with data attribute, but moving this logic to the child element). This way you don't need to rely on data attributes at all and worry about reference check.

Personally, I believe data attributes are quite nice for CSS mostly (for example to show dynamic content through the pseudo elements). Or display some hints for debugging purpose.

Collapse
 
slashnot profile image
slashnot

Why not extract the map function outside of the view and use use callback?

Collapse
 
mysticmanan profile image
Abdul Manan

while you do:
const id = e.currentTarget.dataset.id
you can also access it by doing:
if(e.target.(you can use id, tagName, class whatever you define) == "id, tagName, class"){
const id = e.target.getAttribute('data-user-id')

}

Collapse
 
ryansully profile image
Ryan Sullivan

From a functional programming perspective, wouldn't using dataset open the door to side effects?

Collapse
 
kyakaze profile image
Khoa Dam

Is there any benchmark? The cost of type conversion overhead, longer code, meh.

Collapse
 
19yashu_ profile image
Yashu

i am in that 1%

Collapse
 
kirill_novgorodtsev_f9433 profile image
Kirill Novgorodtsev

Better will be just use $mol

Collapse
 
4umfreak profile image
Mark Voorberg

Interesting idea. This approach has its uses, but there are good alternatives and some good ideas in comments too.

Collapse
 
voidbrain profile image
daniele

Broh, data-* attributes are available since jQuery era.
Do you really need to discover the wheel every 5 years?!?

Collapse
 
spock123 profile image
Lars Rye Jeppesen

I don't use React

Collapse
 
snuffish profile image
Snuffish

Keep the rendering and the actual data/state of things completely seperate. State management has nothing todo with DOM-Rendering (or any graphical interface for that matter).

Collapse
 
amz_dev profile image
AMZ

Just because you can does not mean you should

Collapse
 
ermaneng profile image
Erman Enginler

The writer hatches more complex problems than the mentioned closure problem.