You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`element`: The `element`argument must be a valid React element. For example, it could be a JSX node like `<Something />`, the result of calling [`createElement`](/reference/react/createElement), or the result of another `cloneElement`call.
*`props`: The `props`argument must either be an object or `null`. If you pass `null`, the cloned element will retain all of the original `element.props`. Otherwise, for every prop in the `props`object, the returned element will "prefer" the value from `props`over the value from `element.props`. The rest of the props will be filled from the original `element.props`. If you pass `props.key`or`props.ref`, they will replace the original ones.
***optional**`...children`: Zero or more child nodes. They can be any React nodes, including React elements, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and`false`), and arrays of React nodes. If you don't pass any `...children`arguments, the original `element.props.children`will be preserved.
`cloneElement`returns a React element object with a few properties:
58
+
`cloneElement`返回一个具有一些属性的 React element 对象:
59
59
60
-
*`type`: Same as `element.type`.
61
-
*`props`: The result of shallowly merging `element.props`with the overriding `props`you have passed.
62
-
*`ref`: The original `element.ref`, unless it was overridden by `props.ref`.
63
-
*`key`: The original `element.key`, unless it was overridden by `props.key`.
60
+
*`type`:与 `element.type` 相同。
61
+
*`props`:将 `element.props`与你传递的 `props`浅合并的结果。
62
+
*`ref`:原始的 `element.ref`,除非它被 `props.ref` 覆盖。
63
+
*`key`:原始的 `element.key`,除非它被 `props.key` 覆盖。
64
64
65
-
Usually, you'll return the element from your component or make it a child of another element. Although you may read the element's properties, it's best to treat every element as opaque after it's created, and only render it.
*Cloning an element **does not modify the original element.**
69
+
*克隆一个元素 **不会修改原始元素**。
70
70
71
-
*You should only**pass children as multiple arguments to `cloneElement` if they are all statically known,** like `cloneElement(element, null, child1, child2, child3)`. If your children are dynamic, pass the entire array as the third argument: `cloneElement(element, null, listItems)`. This ensures that React will [warn you about missing `key`s](/learn/rendering-lists#keeping-list-items-in-order-with-key) for any dynamic lists. For static lists this is not necessary because they never reorder.
71
+
*如果已知 children 是静态的,则你应该**将它们作为多个参数传递给 `cloneElement`**,例如 `cloneElement(element, null, child1, child2, child3)`。如果你的 children 是动态的,请将整个数组作为第三个参数传递:`cloneElement(element, null, listItems)`。这确保了 React 会对任何动态列表 [警告你缺少“key”](/learn/rendering-lists#keeping-list-items-in-order-with-key),对于静态的列表,这是不必要的,因为它们不会重新排序。
72
72
73
-
*`cloneElement`makes it harder to trace the data flow, so **try the [alternatives](#alternatives) instead.**
To override the props of some <CodeStepstep={1}>React element</CodeStep>, pass it to `cloneElement` with the <CodeStepstep={2}>props you want to override</CodeStep>:
**Let's walk through an example to see when it's useful.**
95
+
**让我们看一个示例,看看它什么时候有用**。
96
96
97
-
Imagine a `List`component that renders its [`children`](/learn/passing-props-to-a-component#passing-jsx-as-children)as a list of selectable rows with a "Next" button that changes which row is selected. The `List`component needs to render the selected `Row` differently, so it clones every `<Row>`child that it has received, and adds an extra `isHighlighted: true`or`isHighlighted: false`prop:
Instead of using `cloneElement`, consider accepting a *render prop* like `renderItem`. Here, `List`receives`renderItem`as a prop. `List`calls`renderItem` for every item and passes `isHighlighted`as an argument:
The `renderItem`prop is called a "render prop" because it's a prop that specifies how to render something. For example, you can pass a `renderItem`implementation that renders a `<Row>` with the given `isHighlighted` value:
Another approach you can try is to extract the "non-visual" logic into your own Hook, and use the information returned by your Hook to decide what to render. For example, you could write a `useList`custom Hook like this:
0 commit comments