-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Update reference-react.md #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,16 +103,14 @@ React.cloneElement( | |
) | ||
``` | ||
|
||
Clone and return a new React element using `element` as the starting point. The resulting element will have the original element's props with the new props merged in shallowly. New children will replace existing children. `key` and `ref` from the original element will be preserved. | ||
Clone and return a new React element using `element` as the starting point. The resulting element will have the original element's props with the new props merged in shallowly. New children, if passed as `...children` or otherwise `props.children`, will replace existing children. `key` and `ref` from the original element will be preserved unless `props` contains `key` or `ref` (which will overwrite the `key` or `ref` in the cloned element, respectively). | ||
|
||
`React.cloneElement()` is almost equivalent to: | ||
|
||
```js | ||
<element.type {...element.props} {...props}>{children}</element.type> | ||
``` | ||
|
||
However, it also preserves `ref`s. This means that if you get a child with a `ref` on it, you won't accidentally steal it from your ancestor. You will get the same `ref` attached to your new element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a useful sentence. Maybe we could just tweak the wording slightly?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this sentence is very unclear though. Is "child with a Maybe the clearest thing to say would be " There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could replace, "child with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but up above the code example it already says " |
||
|
||
This API was introduced as a replacement of the deprecated `React.addons.cloneWithProps()`. | ||
|
||
* * * | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new wording is informative but it reads a little confusingly to me. How do you feel about just slightly tweaking what was there before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, how can we be precise without being confusing? "explicitly overridden" doesn't make it clear that they can be overridden by mixing
key
orref
in with the other props passed tocloneElement
. To me that's not obvious unless explicitly stated becausekey
andref
are like pseudoprops. I guess the code example does make that clear, if one thinks through what it means.And as far as children goes, "new children will replace existing children" doesn't clarify that existing children can be preserved -- it seems to imply that if
...children
andprops.children
are not provided, then the cloned element will have no children.The fact that
children
can be overridden viaprops.children
instead of...children
is also not obvious at all. I'm trying to fully and correctly document all behavior ofcloneElement
, which really isn't that trivial...