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
Copy file name to clipboardExpand all lines: content/docs/jsx-in-depth.md
+13-22Lines changed: 13 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -245,40 +245,31 @@ function App2() {
245
245
}
246
246
```
247
247
248
-
Spread attributes can be useful when you are building generic containers. However, they can also make your code messy by making it easy to pass a lot of irrelevant props to components that don't care about them. We recommend that you use this syntax sparingly.
248
+
You can also pick specific props that your component will consume while passing all other props using the spread operator.
249
+
This ensures that the component consumes the `kind` prop only, and passes down all other props via `...other`.
249
250
250
-
An alternative to spreading all of your props is to specifically list out all the properties that you would like to consume, followed by `...other`.
251
-
252
-
```js
253
-
const { children, ...other } = props;
254
-
```
255
-
256
-
This ensures that you pass down all the props *except* the ones you're consuming yourself.
In the example above, the `...other` object contains `{ onClick: handleClick(), type: "button" }` but *not* the `children` prop. This makes a component like the `PrimaryButton` more reusable and flexible because it can extract a set of unknown properties like `onClick` in the above example.
269
+
In the example above, the `kind` prop is safely consumed and *is not* passed directly to the `<button>` element in the DOM.
270
+
All other props are passed via the `...other` object making this component really flexible. You can see that it passes an `onClick` and `children` props.
271
+
272
+
Spread attributes can be useful but not to pass invalid HTML attributes to the DOM. They can also make your code messy by making it easy to pass a lot of irrelevant props to components that don't care about them. We recommend using this syntax sparingly.
0 commit comments