Linking an external URL with a button using Material UI - A simple guide

I'm currently exploring Material-UI and testing my web application on localhost. I've added a button that should redirect users to an external URL (currently set as www.google.ca) when clicked. However, instead of redirecting to the correct URL (), clicking the button redirects to http://localhost:3001/www.google.ca. I'm unsure about what might be causing this issue.

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Card from '@material-ui/core/Card';
import CardActionArea from '@material-ui/core/CardActionArea';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import Link from '@material-ui/core/Link';

import waterBottle from '../images/tom.jpg';

const useStyles = makeStyles({
  root: {
    maxWidth: 345,
  },
  media: {
    height: 140,
  },
});

export default function MembersInfo() {
  const classes = useStyles();

  return (
    <Card className={classes.root}>
      <CardActionArea>
        <CardMedia
          className={classes.media}
          image={waterBottle}
          title="Image"
        />
        <CardContent>
          <Typography gutterBottom variant="h5" component="h2">
            Tom Wong
          </Typography>
          <Typography variant="body2" color="textSecondary" component="p">
           Tom is a business administrative student at xxx university.
          </Typography>
        </CardContent>
      </CardActionArea>
      <CardActions>
        
            <Button size="small" color="primary" href="www.google.ca" target="_blank" to = "/url">
            Google
            </Button>
        
       
      </CardActions>
    </Card>
  );
}

Answer №1

If you encounter problems while using the Link component from Material UI, make sure to check the comments for solutions.

It's important to import the Link correctly from material ui, especially after they updated their API. I initially used ('@mui/material/Link') but had to switch back to the old API (@material-ui/core/Link) for it to function properly.

Assuming the import is done right, here's a code snippet that will open Google in a new tab while developing on localhost:

<Link target="_blank" href="http://www.google.com" rel="noreferrer">
View Link
</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

There seems to be a caching issue in ReactJS and Spring Data Rest that could be causing problems with

Encountering an unusual caching problem here. Just recently wiped out my database. While adding new users to the system, an old user mysteriously reappeared. This user has not been recreated and is not in the current database whatsoever. I'm at a lo ...

Creating React Context Providers with Value props using Typescript

I'd prefer to sidestep the challenge of nesting numerous providers around my app component, leading to a hierarchy of provider components that resembles a sideways mountain. I aim to utilize composition for combining those providers. Typically, my pro ...

Unraveling Complex JSON Structures in React

Having trouble reading nested JSON data from a places API URL call? Look no further! I've encountered issues trying to access all the information and nothing seems to be coming through in the console. While unnested JSON is not an issue, nested JSON p ...

sending various properties to a component using the context API

I am working with two different components, <AuthConsumer> and <PeopleConsumer>, which belong to a Higher Order Component (HOC) in the following structure: const withAuth = WrappedComponent => { return () => ( <AuthC ...

Any ideas on how to customize generated JSS class names?

Currently, I am developing a proof of concept micro frontend app using single-spa, react, and material-ui. An issue has arisen where the generated class names from Mui are conflicting due to multiple individual apps being registered within the parent app, ...

Generating React components dynamically can bring flexibility and efficiency to the

Looking for a solution to dynamically render different components based on the arguments passed with data. While using regular or React.createElement(Item) works fine, all other options seem to fail. http://jsfiddle.net/zeen/fmhhtk5o/1/ var React = windo ...

The MUI icon is visible in the developer tools but fails to display on the screen

Recently, I've been troubleshooting MUI icons in React and encountered an issue where my icons weren't displaying properly even after npm installing @mui/icons-material and importing them using the code snippet below: import HomeIcon from "@mui/i ...

Warning from React 17: Unexpected presence of <a> tag inside <div> tag in server-rendered HTML

I've checked out the existing solutions and still can't seem to make it work. components/NavBar.tsx import { Box, Link } from "@chakra-ui/react"; import { FunctionComponent } from "react"; import NextLink from "next/link ...

How can I adjust the font size in a TextField when it is in focus?

As a novice in ReactJS, I am currently utilizing materia-ui to design a page. I am looking to make a custom change on a TextField where the font size adjusts when text is entered. However, adjusting the font size creates too much space between the label a ...

What is the correct way to input the 'name' HTML attribute in an Ant Design select element?

I am facing an issue with the 'name' attribute in my Ant Design Select component. When trying to set the 'name' attribute, I encountered an error message that is causing issues. https://i.stack.imgur.com/Lzb4t.png Ant Design Select Co ...

Error: Unable to retrieve current location using 'Geolocation' in React

import "./App.css"; import { useState, useEffect } from "react"; function App() { const [lat, setLat] = useState(null); const [long, setLong] = useState(null); const [data, setData] = useState(null); const [error, setError] = u ...

Guide to grouping polygon shapes using react-leaflet?

I'm currently exploring options for clustering polygons with react-leaflet v4 and react-leaflet-markercluster. Despite my efforts, I have been unable to locate recent examples showcasing how this can be accomplished. Therefore, I am seeking assistance ...

Enhance the language with react-intl and MobX State Tree integration

Currently, I am integrating react-intl with Mobx state tree for the first time. Within my project, there are two buttons located in the header section - one for 'it' and the other for 'en'. Upon clicking these buttons, the selected lan ...

What steps can I take to ensure that AstroJS components do not conceal SVG elements when the SVG is incorporated into another file with client:load?

Currently, I am developing a weather application using Astro.js in conjunction with React. One of the features includes an SVG component that serves as the project logo and is implemented in the initial page loader. Upon the page loading, the SVG functions ...

Leveraging the power of Material-UI and React, modify the appearance of the "carrot" icon within the

Currently implementing MUI's textfield based on the information found at this link: https://mui.com/components/text-fields/. While there are numerous resources available for changing the font of the input text, I have not come across any documentation ...

Utilizing GraphQL API in a React frontend: A Guide

I'm looking to build a frontend for my Graphcool API endpoint and need some guidance on how to get started with ReactJS-Graphcool. Any references or tips would be greatly appreciated! ...

Step-by-step guide on integrating StyleX into your fresh React project

As I delve into my new project, incorporating StyleX has proven to be a bit challenging especially when working with NextJS. I find myself grappling with configuring the "next.config.js" file without causing conflicts with the existing "babel.config.js" f ...

Discover the art of highlighting errors with React-hook-form and MUI React

My code consists of the following component: const App = () => { const formProps = useForm({ mode: "onBlur", }); const { handleSubmit, formState, register, watch, reset } = formProps; return ( <FormProvider {...formProps}> & ...

Tips on updating the specific index of an array in React Native using setState

I am currently working on a project where I have an array of objects that I need to render as buttons. The requirement is that when a user clicks on a button, only that specific button should change its background color, similar to a toggle switch. Event ...

Mui Drawer transitions

I'm currently working on a project for a company and using Mui drawer with a variant drawer. However, I've noticed that the opening and closing of the drawer happens very quickly and I would like it to have a slower transition. The usage of the ...