Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With React JSX, does placing a static value prop in curly braces incur any overhead?

Tags:

reactjs

jsx

I know we can pass dynamically changing props in a JSX element like so:

<label htmlFor={dynamicId}/>

And we can pass static values like so:

<label htmlFor="staticId"/>

But what about the following:

<label htmlFor={'staticId'}/>

Is there an (albeit minor) performance overhead in the last line (internal React caching mechanism/ new string on every render)? Is the last line of code a bad practice or does it just not matter?

like image 805
webketje Avatar asked Sep 17 '25 11:09

webketje


2 Answers

They're exactly equivalent. You can use Babel's playground to try it out:

[
  <label htmlFor="staticId"/>,
  <label htmlFor={'staticId'}/>
]

transpiles to

[
  React.createElement("label", { htmlFor: "staticId" }),
  React.createElement("label", { htmlFor: "staticId" })
];
like image 137
AKX Avatar answered Sep 20 '25 02:09

AKX


No. It doesn't. React performance is very fast.

Using curly brace, you're telling to React to try to evaluate it and find a string. You're just taking a step longer.

At the end, result will be the same.

Though, I would recommend you to use without the curly brace if it is just a string and not a variable.

like image 24
Bhojendra Rauniyar Avatar answered Sep 20 '25 03:09

Bhojendra Rauniyar