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.

Link as styled component

See original GitHub issue

I followed #799 but couldn’t get a clean way to have a styled-component link, I had to duplicate the href:

const StyledLink = styled.a`
  color: red;
  background: blue;
`

export default ({ href, name }) => (
  <Link prefetch href={href}>
    <StyledLink href={href}>{name}</StyledLink>
  </Link>
)

What’s the proper way to make styled-component links?

Using next.js 2.3.1.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

97reactions
bitwisemanagercommented, Jul 9, 2017

Hey guys. At version 3.0.1-beta.13+, you could set passHref to Link (as a boolean property) to expose its href to styled-components (or any other library that wraps its <a/> tag).

Using the first example of issue: it could be done this way:

const StyledLink = styled.a`
  color: red;
  background: blue;
`

export default ({ href, name }) => (
  <Link prefetch href={href} passHref>
    <StyledLink>{name}</StyledLink>
  </Link>
)
40reactions
kennylavendercommented, May 18, 2017

You can pass a component to styledcomponents instead of using a tag https://github.com/styled-components/styled-components#third-party-components

import Link from 'next/link'
const StyledLink = styled(Link)`
  color: red;
  background: blue;
`

Use it

export default () => (<div><StyledLink href="mylink">MyText</StyledLink></div>)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style React Router links with styled-components
Using styled-components helps you keep track of which components are rendered on a page and injects only those components' styles. When combined ...
Read more >
How to style your React-Router links using styled-components
METHOD 2: Styling links using 'styled.​​ If you are familiar with styled components, you should know that styled is like the very basic...
Read more >
Styling a Router Link with styled-components - Stack Overflow
What is the best way to style the Link using the styled-components library in the code that follows. I can find lots of...
Read more >
Basics - styled-components
styled -components utilises tagged template literals to style your components. It removes the mapping between components and styles. This means that when you're ......
Read more >
Style active state of Links in Styled Components - Medium
A Styled Component passes all HTML attributes if it is a simple element, like a div or button or ..., and all props...
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