Styling <Link> component with styled-components: A step-by-step guide

Utilizing the Link component from @material-ui/core/Link in my TypeScript code was initially successful:

<Link href="#" variant="body2">
  Forgot?
</Link>

However, I am exploring the transition to styled-components located in a separate file. Presently, I'm attempting to incorporate this approach (for example: https://styled-components.com/docs/basics):

const Link = ({ className, children}) => (
  <a className={className}>
    {children}
  </a>
);

export const StyledLink = styled(Link)`
  href: #;
  variant: body2;
`;

coupled with:

<StyledLink>Forgot?</StyledLink>

Nevertheless, I'm encountering errors related to className and children, specifically

Binding element 'children' implicitly has an 'any' type.ts(7031)
. Even after specifying any, it remains unresolved.

What is the appropriate methodology for integrating styled-components in this scenario? Alternatively, are there any other alternatives for css-in-js implementation?

Answer №1

This piece of code is functioning perfectly and does not trigger any warnings from the typescript compiler

import styled from "styled-components";

const Link = ({
  className,
  children
}: {
  readonly className: string;
  readonly children: React.ReactElement;
}) => (
  <a href="/" className={className}>
    {children}
  </a>
);

export const StyledLink = styled(Link)`
  href: #;
  variant: body2;
  color: red;
`;

function App() {
  return (
    <StyledLink className="classic">
      <div>Forgot?</div>
    </StyledLink>
  );
}

Answer №2

To create your own customized Link component, follow the example code snippet below:

// CustomLink.js
import { Link } from '@material-ui/core/Link';
import styled from 'styled-components';

export default styled(Link)`
  display: block;
  color: #F51963;
  text-decoration: none;
`;

Answer №3

If you want to enhance your divs or buttons, consider using the <Link> element

<Link><button className="Btn">Enhance?</button></Link>

There are two simple ways to customize the style of the button / div elements:

<Link><button className="Btn" style={{backgroundColor: 'red'}}>Enhance?</button></Link>

Alternatively, you can import a separate CSS file for styling purposes

import styles from './ComponentName.module.css'

Then apply the styles like this:

<Link><button className={styles.Btn}>Enhance?</button></Link>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Formik struggles to retrieve the chosen value from a dropdown selection

I'm currently using react, but I'm having trouble incorporating the selected select into the form data in Formik. Here is my form: <Card.Body> <Formik validationSchema={signUpS ...

There seems to be an issue with accessing the requested page,

Having some trouble with routing in external files and getting a 'Cannot Get /' error. Can anyone help me figure out what I'm doing wrong? Here is my server.js file: const express = require('express'); const mongoose = require(&a ...

Introducing Block Insert feature in React Draft-js - a powerful

How the Process Works: Upon hitting the spacebar, the Draft-JS editor queries the text content for a specific word. Subsequently, all instances of that word are enveloped in tags. The HTML is then converted back and the state of the Draft-JS editor is upd ...

Monitor changes in the service with React and send the refreshed data to the Admin dataProvider in a separate component

Just starting out with React and I'm looking to implement a feature to listen for incoming data from a service. Here's the code snippet of the service: import axios from "axios" const base_url = 'https://localhost:5001/OrderItem&a ...

What are the steps for integrating a date and time picker bootstrap plugin?

Referencing a tutorial from http://www.w3schools.com/bootstrap/bootstrap_modal.asp: <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <div id="myModal" class="modal fade" role= ...

What is the process for creating a unique Vee-Validate rule in TypeScript?

I am in the process of developing a unique VeeValidate rule for my VueJS component written in TypeScript. This rule is designed to validate two fields simultaneously, following the guidelines outlined in VeeValidate - Cross Field Validation. Here is a snip ...

Refresh Search Suggestions in React Material Design Interface

Is there a way to reset the state_id when I select a value from the Autocomplete for city_id? Currently, it doesn't clear out automatically. If you want to see the code in action, please click on the link below: CLICK HERE CODE <Autocomple ...

Having trouble importing Formik properly

Utilizing react-native, I installed formik in my project using the command: "npm add formik yup @material-ui/core @material-ui/icons" on vs code. However, upon reloading my project after creating a form, an error message appeared: "formik c ...

Is it possible that the method of wrapping options using an array is not functioning correctly in the quiz app being managed in React?

I need your help in figuring out where I've made a mistake. The following line is causing an error: const choices = Array.forms(document.querySelectorAll('.choice')); console.log(choices); ...

Creating a JSON structure using an array in Typescript

Here is an example of my array structure: [ { "detail": "item1", "status": "active", "data": "item1_data" }, { "detail": "item2", "status": ...

Can you explain the purpose and function of stub.callsArg(index) feature in Sinon.JS?

Confusion has set in as I try to make sense of this. According to the documentation: stub.callsArg(index) - This command prompts the stub to execute the callback function found at the specified index. For instance, using stub.callsArg(0); will trigger the ...

What is the best way to transform this JSON data into an array of key-value pairs in JavaScript?

Dealing with nested JSON data can be challenging, especially when trying to extract key-value pairs efficiently. If anyone has suggestions on how to simplify this process and improve readability, please share your insights. The goal is to transform the ne ...

How can we improve app functionality for applications created using React Native?

I am looking for a way to automatically update my react-native application on my customer's device. How can I send updates - is it through push notifications with a link to download and install the new version of the app, or is there a better method? ...

Steps for accessing specific menu div on a new tab

I'm facing an issue with my one page website. Whenever I click on a menu item, it directs me to a specific div tag on the page. However, if I right-click on a menu item and select 'open in new tab', it opens the URL "www.mysite.com/#" instea ...

What is the reason behind VueJs not having built-in support for multiple select options?

Recently delving into the world of vue, I encountered a challenge while working on an update form. When trying to pre-select multiple options using the selected attribute, I noticed that only the last option was being selected. Upon further investigation, ...

"Exploring the incredible powers of Ionic2, Angular2, HTTP requests, and

Despite all the research I've done on observables, I still struggle to grasp how they function. The HTTP request code snippet is as follows: import { Component, OnInit, Injectable } from '@angular/core'; import { Http, Response, Headers, R ...

You can't observe the behavior of simulated functions in a class with a manually created mock

Kindly note that I have set up a comprehensive Github repository where you can download and explore the content yourself here I am currently working on mocking a non-default exported class within a module using a manual mock placed in the folder __mocks__ ...

When the update button is clicked, the textfield is hidden; it reappears upon refreshing the page

Our marketplace multi-vendor site on Magento allows sellers to list their products for sale. The frontend displays the price using the following code: Phtml <input onFocus="showPriceCancel('<?php echo $products->getId(); ?>');" clas ...

What is the reason for NextJS/React showing the message "You probably didn't export your component from the file where it was declared"?

What's the Issue The error code I am encountering Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the ...

Is there a more efficient method for horizontally and vertically aligning in this particular scenario using HTML and CSS?

Embarking on my HTML/CSS journey, I am taking on the challenge of creating my very first website. Currently tackling the final CSS project for Codecademy. I've been tinkering with this task for what feels like an eternity and just can't seem to ...