Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useMemo for string constants

in some code i have seen the below usage of useMemo. is this valid? i've been wondering what is advantage of this and why can't we simply use const?

const id = React.useMemo(() => "sort_id", []);
like image 319
user3607901 Avatar asked Oct 26 '25 17:10

user3607901


1 Answers

const gets reinstantiated on every render, whereas useMemo will hold the same reference until the component is unmounted/remounted.

like image 138
jmkmay Avatar answered Oct 29 '25 07:10

jmkmay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!