I have seen some apis in ReactJs to allow keys, e.g. <React.Fragment>
, but at the same time <React.Suspense>
doesn't have keys.
Now specifically why does React.createPortal
allow key like list items?
My research: I didn't find much on SO or google, but I found one discussion on github, which seem to imply that keys were added in the documentation after 18 April 2023.
Indeed, I find the current React documentation a little thin in some areas myself.
I also can't find an explicit statement regarding the
key
property in JSX elements there (I can't prove the non-existence of anything, so forgive me if I missed it and feel free to comment).React.createElement()
UPDATE 2023-07-19:
This section about
createElement()
is probably outdated.JSX syntax is apparently not converted to
createElement
anymore (React v18), but instead to a call to an internaljsx()
function, which seems not to be supposed to be called by the developer, I believe defined in /src/jsx/ReactJSXElement.js:Previous answer:
Anyway, knowing that JSX is converted to
React.createElement(...)
calls (which, to my surprise, is mentioned under "Legacy React APIs"), I can see thatprops
passed tocreateElement
will handle aprops.key
property in a special way:Also note the legacy
createElement
documentation:Deduction
That is a general statement about
createElement
, so every element created bycreateElement
should allow thekey
property.Assuming that it is still true that every JSX element is a
createElement
call, as it was in the past (the word "legacy" suggests it might change), this implies that every JSX element accepts akey
prop.So it seems "clear" to me that every JSX element accepts a
key
, but in my opinion it should be stated more explicitly.<React.Fragment>
I guess that the documentation mentions
key
in<React.Fragment>
explicitly, because it's a common question how to add akey
to a fragment, because a fragment is more commonly written using the empty-brackets-syntax<>
, where you can not add any properties. But that's just a guess.React.createPortal()
React.createPortal() is different, because it is not JSX syntax, so it needs a specific documentation.
Rendering Lists
The section "Rendering Lists" mentions
key
properties, but I find this not sufficient to answer your question.It is solely focused on keys in "list items" / "collections" / "arrays", and doesn't say anything about the
key
property in general.