What is the purpose of adding {...props} right before sx? #1310
-
Hi, Reading through the docs I came across this syntax, but am not clear on why it exists. Also, if I exclude it, it still works fine.
Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This syntax isn’t Theme UI-specific, it’s using a JavaScript “spread operator” to “forward” all props. That means if you’re passing children or DOM attributes or style props, they’ll be rendered on that styled div instead of ignored. It’s a common pattern with UI components (every one of Theme UI’s internal components uses it), so we don’t restrict what kinds of attributes you can use on those elements. You can read more about its usage in React here: https://gist.github.com/sebmarkbage/07bbe37bc42b6d4aef81#spread-attributes & on the operator itself: https://www.joshwcomeau.com/operator-lookup?match=restspread |
Beta Was this translation helpful? Give feedback.
This syntax isn’t Theme UI-specific, it’s using a JavaScript “spread operator” to “forward” all props. That means if you’re passing children or DOM attributes or style props, they’ll be rendered on that styled div instead of ignored. It’s a common pattern with UI components (every one of Theme UI’s internal components uses it), so we don’t restrict what kinds of attributes you can use on those elements.
You can read more about its usage in React here: https://gist.github.com/sebmarkbage/07bbe37bc42b6d4aef81#spread-attributes
& on the operator itself: https://www.joshwcomeau.com/operator-lookup?match=restspread