Defualt props on styled element (.attrs)
See original GitHub issueDo you want to request a feature or report a bug? feature
What is the current behavior?
Using the styled
variable there is no way to set default props/attributes on the elements. In styled-components
there is a way using the .attrs
property such as
styled.img.attrs({
src: require('...')
})`
width:100%;
`
Have I missed some documentation of this (or a similar) feature or is this not (yet?) supported by linaria? Or is there another best practise of this?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Quick Start Guide to Attrs in Styled Components - Scalable CSS
Use Case 1: Defining Default Attributes. Here I've put together a simple button styled component: import styled from 'styled-components';.
Read more >Advanced Usage - styled-components
We are passing a default theme for Buttons that arent wrapped in the ThemeProvider. Button.defaultProps = {. theme: {. main: "palevioletred". }.
Read more >defaultProps with styled-components - Stack Overflow
I just want to add a defaultProps in case I dont add the “primary” or any other one. For example props.primary be my...
Read more >Styled components, the styling library for your React apps you ...
The styled-component library can apply styles conditionally by looking for the occurrence of a specified attribute on our element.
Read more >Complete Guide On How To Use Styled-components In React
So let's say you wanna override default props passed into a component or create one in the styled component, you'd need to use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, no surprise that this requires extra work and also may become a problem by adding extra complexity to the transpiler(?). However, personally I think this is quite an essential feature that might be a deal breaker for me. Despite the other amazing features of Linaria, clean and concise code is prio 1 for me.
If I use it often or not I think is quite irrelevant because I don’t want to trade your time against only my convenience. My main purpose was only to make the library better in general for any developer.
But to answer your question, yeah I use this feature in pretty much every other file. This mainly regards very simplistic components (such as in the example above), but also sometimes as a hoc using the
styled(Component)
-function.But once again; This is not a demand, I’m only saying that I think you might have discarded this feature a bit quick.
Yeah, the
attrs
prop is useful if you want to derive props from props. However, implementing it also means extra work.For a small amount of usage, usually, an HOCs is fine, or we can move the computation to an external function.
Do you need this functionality often?