Skip to content

Commit 5bebe99

Browse files
authored
Explain how ref and key are treated with cloneElement (#1394)
1 parent 669890b commit 5bebe99

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

content/docs/reference-react.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,20 @@ Code written with [JSX](/docs/introducing-jsx.html) will be converted to use `Re
175175
```
176176
React.cloneElement(
177177
element,
178-
[props],
178+
[config],
179179
[...children]
180180
)
181181
```
182182

183-
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.
183+
Clone and return a new React element using `element` as the starting point. `config` should contain all new props, `key`, or `ref`. 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 if no `key` and `ref` present in the `config`.
184184

185185
`React.cloneElement()` is almost equivalent to:
186186

187187
```js
188188
<element.type {...element.props} {...props}>{children}</element.type>
189189
```
190190

191-
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.
191+
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. The new `ref` or `key` will replace old ones if present.
192192

193193
This API was introduced as a replacement of the deprecated `React.addons.cloneWithProps()`.
194194

0 commit comments

Comments
 (0)