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
Let's say you have a `button` and an `a` tag. You want them to share the exact same style. This is achievable with `.withComponent`.
235
-
```JSX
236
-
constButton=styled.button`
237
-
background: green;
238
-
color: white;
239
-
`
240
-
constLink=Button.withComponent('a')
241
-
```
242
-
243
233
### Polymorphic `as` prop
244
234
If you want to keep all the styling you've applied to a component but just switch out what's being ultimately rendered (be it a different HTML tag or a different custom component), you can use the "as" prop to do this at runtime. Another powerful feature of the `as` prop is that it preserves styles if the lowest-wrapped component is a `StyledComponent`.
245
235
@@ -260,6 +250,16 @@ Using the `as` prop in another template/component would be as shown below.
260
250
```
261
251
This sort of thing is very useful in use cases like a navigation bar where some of the items should be links and some just buttons, but all be styled the same way.
262
252
253
+
### withComponent
254
+
Let's say you have a `button` and an `a` tag. You want them to share the exact same style. This is achievable with `.withComponent`.
255
+
```JSX
256
+
constButton=styled.button`
257
+
background: green;
258
+
color: white;
259
+
`
260
+
constLink=Button.withComponent('a')
261
+
```
262
+
263
263
### injectGlobal
264
264
265
265
A helper method to write global CSS. Does not return a component, adds the styles to the stylesheet directly.
0 commit comments