Ask Your Question
0

How to use styled-components to customize the appearance of a Router Link?

asked 2022-10-28 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-12-01 15:00:00 +0000

david gravatar image

To use styled-components to customize the appearance of a Router Link, you can create a new component that extends the original Router Link, and use the styled-components syntax to add your desired styling.

Here is an example:

import React from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";

const StyledLink = styled(Link)`
  color: #fff;
  background-color: #343a40;
  padding: 10px;
  border-radius: 5px;
  text-decoration: none;

  &:hover {
    background-color: #212529;
  }
`;

export default function CustomLink(props) {
  return <StyledLink {...props} />;
}

In this example, we create a new component called CustomLink, which extends the Link component from react-router-dom. We then use styled-components to add some CSS styles to the StyledLink component.

You can use this CustomLink component wherever you would normally use the Link component, and your custom CSS styles will be applied. For example:

import React from "react";
import CustomLink from "./CustomLink";

export default function MyComponent() {
  return (
    <div>
      <CustomLink to="/">Home</CustomLink>
      <CustomLink to="/about">About</CustomLink>
      <CustomLink to="/contact">Contact</CustomLink>
    </div>
  );
}

In this example, we use the CustomLink component to create three links, each with their own destination URL and text content. When rendered, these links will have the custom styling we defined in the StyledLink component.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-28 11:00:00 +0000

Seen: 10 times

Last updated: Dec 01 '21