Skip to content
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

Update reference-react.md #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jedwards1211
Copy link
Contributor

The docs don't mention that key and ref can be overwritten with cloneElement(element, {key: ..., ref: ...}) and children can also be overwritten with cloneElement(element, {children: ...}).

The docs don't mention that `key` and `ref` can be overwritten with `cloneElement(element, {key: ..., ref: ...})` and `children` can also be overwritten with `cloneElement(element, {children: ...})`.
@reactjs-bot
Copy link

Deploy preview ready!

Built with commit 73cc5ea

https://deploy-preview-237--reactjs.netlify.com


`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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Except that it also preserves refs by default. 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.

Copy link
Contributor Author

@jedwards1211 jedwards1211 Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this sentence is very unclear though. Is "child with a ref on it" referring to a child of the cloned element or the cloned element itself? In any case, it's basically just restating what the sentence before the code example says.

Maybe the clearest thing to say would be "cloneElement preserves the refs of all descendants of the element being cloned".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could replace, "child with a ref on it" with something more like, "if you clone an element that has a ref".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but up above the code example it already says "key and ref from the original element will be preserved."

@@ -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).
Copy link
Contributor

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?

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 unless they are explicitly overridden.

Copy link
Contributor Author

@jedwards1211 jedwards1211 Nov 2, 2017

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 or ref in with the other props passed to cloneElement. To me that's not obvious unless explicitly stated because key and ref 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 and props.children are not provided, then the cloned element will have no children.

The fact that children can be overridden via props.children instead of ...children is also not obvious at all. I'm trying to fully and correctly document all behavior of cloneElement, which really isn't that trivial...

jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request Jul 1, 2020
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
…actjs#237)

* docs(cn): translate content/blog/2016-07-11-introducing-reacts-error-code-system.md to chinese

* docs(cn): format style in content/blog/2016-07-11-introducing-reacts-error-code-system.md

* Update content/blog/2016-07-11-introducing-reacts-error-code-system.md

Co-Authored-By: Yuqing Chen <mr.chenyuqing@live.com>

* Update content/blog/2016-07-11-introducing-reacts-error-code-system.md

Co-Authored-By: Yuqing Chen <mr.chenyuqing@live.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants