question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defualt props on styled element (.attrs)

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SchlagerKhancommented, Mar 13, 2019

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.

0reactions
satya164commented, Mar 13, 2019

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found