The type of link component that is passed in as the 'component' prop

I have created a custom Button Function Component in TypeScript using Material-UI as the base. After styling and adding features, I exported it as my own Button. Now, when I try to use this Button component in conjunction with the Link component from react-router-dom by passing it as a 'component' prop to the button, I am unsure about what type to use in the ButtonProps interface. Also, should the 'to' prop be specified as a string?

component/button.tsx

import * as React from 'react';
import { Button as BaseButton } from '@material-ui/core';

interface ButtonProps {
  component: ???;
  to: string;
}

export const Button: React.FC<ButtonProps> = ({ component, to }) => (
  <BaseButton component={component} to={to} />
)

.

some other place

import { Button } from 'components/button';
import { Link } from 'react-router-dom';

(...)
  <Button component={Link} to={'/'} />
(...)

Answer №1

< Link > tags serve as functional elements rather than visual components, designed for enhancing navigation capabilities. material-ui is geared towards a contrasting purpose thus facilitating seamless navigation like so


    export const Button: React.FC<ButtonProps> = ({ to }) => 
      <Link to={to}
        <BaseButton /> // customized styled component
      </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

The switch statement and corresponding if-else loop consistently produce incorrect results

I'm currently facing an issue where I need to display different icons next to documents based on their file types using Angular framework. However, no matter what file type I set as the fileExtension variable (e.g., txt or jpg), it always defaults to ...

Responsive date picker component using Material UI

Currently, I am diving into the material UI component library. After going through the documentation, I came across the responsive method for the date picker (see here). It explains that we can make the date picker responsive using the desktopModeMediaQu ...

Troubleshooting: SwipeableDrawer's edge swipe feature is not visible on Material UI

Looking to incorporate Swipeable edge into a SwipeableDrawer: Browse the example here The intention is for the edge to remain visible even when the drawer is closed, allowing users to click on it and slide the drawer open. However, in my application, t ...

Encountering an ERR! message of missing script when trying to run the <npm start> command

Each time I attempt to run npm start, the frustrating "npm ERR! missing script: start" error appears. I have exhaustively attempted to modify the "start" line in the package.json file following all available suggestions on Google dating back a year. In a ...

Ways to avoid Next.js from creating a singleton class/object multiple times

I developed a unique analytics tool that looks like this: class Analytics { data: Record<string, IData>; constructor() { this.data = {}; } setPaths(identifier: string) { if (!this.data[identifier]) this.da ...

How can I display the post title in the URL instead of the ID using Next.js and ensure it stands out?

I've been trying to figure this out, but all the examples I found were in Laravel and PHP. I need to know how to do this in Next.js. So far, I understand how to create dynamic routes in Next.js using the typical format like this ... /pages/post/[pos ...

I'm looking to integrate Jest in my React project with npm. How can I achieve

I've developed my application with create react app and npm. While reviewing the official documentation for jest at https://jestjs.io/docs/tutorial-react, I noticed that they only provide information on testing CRA apps using yarn. Does this suggest t ...

Ways to conditionally display a component upon clicking a different component in React

I am facing an issue with a component that renders another component, specifically related to the 'else if' statement in the render function. constructor(props) { super(props); this.state = { fieldId: "", ...

Solving the Problem of Card Spacing in React Material UI

I am currently incorporating Material UI into a react application. Utilizing Material UI component cards, I have encountered an issue where unwanted white space appears on the right edge of the page. My objective is to introduce gaps between "neighboring" ...

Is there a glitch in my redux-saga?

After developing a React Native app, I encountered an issue with my index.ios.js file where I was adding middlewares to the store. import React, { Component } from 'react' import { AppRegistry, } from 'react-native' import { Provider ...

Issue encountered while setting up controls and AbstractControls in form development

Here is a snippet of code showing how I create and manipulate a form in Angular: this.myForm = new FormGroup({ points: new FormArray([ new FormGroup({ date: this.date, startTime: new FormControl(null, Val ...

Downloading fonts from Google Fonts is always a struggle when using Next.js

After initializing a fresh Next.js project using create-next-app, I managed to successfully launch it with npm run dev. However, an issue arises every time Next.js boots up, displaying the following error: FetchError: request to https://fonts.gstatic.com/ ...

Unexpected error in Jest Test Suite: Problem with Mocking ReactReduxContext's displayName

While running a Jest test suite, a critical error occurred regarding a TypeError with setting the displayName property on an undefined object. This issue popped up during the mocking of the ReactReduxContext from the react-redux library. The error message ...

Encountering an issue while setting up a Next.js project, I am struggling to find a solution despite trying various methods

I attempted to install Next.js, and even updated my npm version in order to create a new React project with the latest Next.js. However, I kept encountering the same error. Aborting installation. Unexpected error. Please report it as a bug: Error: spawn ...

Next.js dynamic routes are not functioning properly upon page refresh

I am currently encountering an issue with dynamic routes in Next.js. Within my application, I am utilizing getStaticPaths, getStaticProps, and the following code snippet: <Link href={`/offers/[id]?id=${offer.id}`} as={`/offers/${offer.id}`} > ...

Mastering the Art of Concise Writing: Tips to

Is there a way to write more concisely, maybe even in a single line? this.xxx = smt.filter(item => item.Id === this.smtStatus.ONE); this.yyy = smt.filter(item => item.Id === this.smtStatus.TWO); this.zzz = smt.filter(item => item.Id == ...

Adding an icon to the Autocomplete TextField in MUI v5 in Reactjs: A step-by-step guide

I created an autocomplete feature with the code below and attempted to display an icon after selecting each item, but unfortunately, it didn't work as expected! import { useState } from 'react' import { TextField,InputAdornment ,Autocomplete ...

Encountering an error with the React Material-UI Datagrid after completing the app development process

I'm facing an issue now. Initially, the console was fine before building my App. However, after building it, the console is logging an error. view image here In my opinion, the cause of this error lies in the row data within the Data-grid. The consol ...

I am struggling to locate the module '@next/env' in Next.js Vercel

After successfully developing a Next.js app on my local machine, I encountered an issue when trying to deploy it on Vercel - it kept giving me the error message Cannot find module '@next/env'. You can see the full error message here. I suspect t ...

The file functions/lib/functions/src/index.ts is missing, preventing the deployment of Cloud Functions

Whenever I attempt to deploy my Firebase cloud functions, I encounter the following error. Expected outcome: Successful deployment of functions. Error: Error: An issue occurred while reading functions/package.json: functions/lib/index.js is missing and ...