What could be causing my React button to stop functioning as a link to an external website when using anchor tags?

I recently created some buttons for my website using React. Initially, everything was working perfectly with the buttons, but suddenly they stopped functioning. Strangely, I didn't make any alterations to the code and I'm puzzled as to why they are no longer working. Below is the button I imported and used in the "About.js" file:

<Button text="Join Our Discord" link="https://discord.gg/Zrk8kXfs" />

And here is the code snippet where I defined the button in the "Button.js" file:

import React from "react";
import styled from "styled-components";

const Btn = styled.button`
  display: inline-block;
  background-color: ${(props) => props.theme.text};
  color: ${(props) => props.theme.body};
  outline: none;
  border: none;

  font-size: ${(props) => props.theme.fontsm};
  padding: 0.9rem 2.3rem;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;

  &:hover {
    transform: scale(0.9);
  }

  &::after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    border: 2px solid ${(props) => props.theme.text};
    width: 100%;
    height: 100%;
    border-radius: 50px;
    transition: all 0.2s ease;
  }

  &:hover::after {
    transform: translate(-50%, -50%) scale(1);
    padding: 0.3rem;
  }
`;

const Button = ({ text, link }) => {
  return (
    <Btn>
      <a href={link} aria-label={text} target="_blank" rel="noreferrer">
        {text}
      </a>
    </Btn>
  );
};

export default Button;

I would greatly appreciate any assistance or insights on why the buttons are no longer working.

Answer №1

For incorporating links in your React application, start by installing the react-router-dom package with npm i react-router-dom

import { Link } from "react-router-dom";

<Link to="https://discord.gg/Zrk8kXfs">
  <Button>Join Our Discord</Button>
</Link>

Answer №2

Avoid placing the a tag inside the button element to prevent screen reader confusion in certain browsers. Instead, utilize the a tag for its intended purpose.

Here is the revised code snippet:

import React from "react";
import styled from "styled-components";

const StyledLink = styled.a`
  display: inline-block;
  background-color: #efefef;
  color: ${(props) => props.theme.body};
  outline: none;
  border: none;
  text-decoration: none;
  font-size: ${(props) => props.theme.fontsm};
  padding: 0.9rem 2.3rem;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;

  &:hover {
    transform: scale(0.9);
  }

  &::after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    border: 2px solid ${(props) => props.theme.text};
    width: 100%;
    height: 100%;
    border-radius: 50px;
    transition: all 0.2s ease;
  }

  &:hover::after {
    transform: translate(-50%, -50%) scale(1);
    padding: 0.3rem;
  }
`;

const Button = ({ text, link }) => {
  return (
    <StyledLink href={link} aria-label={text} target="_blank" rel="noreferrer">
      {text}
    </StyledLink>
  );
};

export default Button;

Answer №3

To turn a button into a link in React, simply wrap the button in an <a> tag or use a Link component if utilizing react router. When the button is clicked, the browser will navigate to the specified page. App.js

https://bobbyhadz.com/blog/react-button-link#:~:text=To%20use%20a%20button%20as%20a%20link%20in,browser%20to%20navigate%20to%20the%20specified%20page.%20App.js

<Link to="https://discord.gg/Zrk8kXfs">
  <Button text="Join Our Discord" />
</Link>

LOL, I was actually about to mention the same thing

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

Apache causes HTML download tag to malfunction

I have an HTML file that includes the following code: <a href="/Library/WebServer/Documents/file.zip" download="file.zip"> Download here </a> When I test this HTML page on Chrome, it successfully allows me to download the file. However, when ...

Tips for displaying the product title of an item in the Woocommerce shopping cart

Is there a way for me to display the title of the product in my Woocommerce store's cart on the checkout page? My store only allows one product in the cart at a time, and I want to customize the checkout page to show the product title. I've tri ...

React strict mode and Material UI console notifications

Just like a rapidly growing application can make the console as dirty as a footballer's shirt. What am I trying to convey? When using Material UI in strict mode, warnings such as FindDomNode may appear, or it may ask you to use strings instead of bo ...

Maximizing Efficiency: Sending Multiple Responses during computation with Express.js

Seeking a way to send multiple responses to a client while computing. See the example below: app.get("/test", (req, res) => { console.log('test'); setTimeout(() => { res.write('Yep'); setTime ...

CSS based on conditions for 2 specific divs

Hello, I am working on an HTML page where I need to apply CSS only if certain conditions are met. The div with the id "flipbook-page3-front" will always be present on this HTML page. Sometimes, another div with the id "pagesFlipbook" may also appear on t ...

"What are the steps to configure an ASP.NET/React application to serve a Single Page Application from a sub

I have a stock aspnetcore and reactjs application that was created using the starter template (dotnet new react). My goal is to serve the Single Page Application (SPA) from a subpath of the root URL. For example, instead of the sample app being accessed at ...

"Enhancing Layouts with Bootstrap: Implementing Spacing Between

Is there a way to add additional margin space between columns in my Bootstrap grid layout? I attempted to use a custom CSS class like this .classWithPad { margin: 10px; padding: 10px; } However, this did not resolve my issue. I also experimented w ...

JavaScript issue: "No relay configuration found" error specifically occurs in Internet Explorer versions 7 and 8

Encountering issues with loading JavaScript only on specific pages in Internet Explorer. Safari, Firefox, and Chrome render the page correctly. Debugging revealed the following errors: 1) No relay set (used as window.postMessage targetOrigin), cannot send ...

Hover Effect for Bootstrap Gallery

I've created a hover effect for my gallery that is embedded in a Bootstrap Grid. Although the hover effect itself works fine, on some screen sizes, the overlay ends up covering the gallery images. Does anyone have any ideas, solutions, or hints to so ...

Utilize Rails file_field input as a clickable button

Currently, I have a button that needs to trigger the action associated with a file_field in my Rails application. Below is the erb code I am using: <label for='file-input'> <span class='btn btn-success' style='max-width ...

difficulty encountered during json parsing

I need help accessing displayName in req When I use this code snippet: console.log(req.session.passport.user._raw) The following information is displayed: { "kind": "plus#person", "etag": "\"ucaTEV-ZanNH5M3SCxYRM0QRw2Y/XiR7kPThRbzcIw-YLiAR ...

Tips to center a circular progress bar in Material UI

Does anyone know how to properly center-align a circular progress bar in a login form using material UI? I'm having trouble with it not being positioned correctly: screenshot {isLoading && <CircularProgress />} {user?.ema ...

The alignment issue persists with the menu header, as the div is not functioning correctly

I've been struggling to properly align my menu on the website. As it stands, when a user arrives and is not logged in, they see the header below: https://i.stack.imgur.com/mTKse.png Once the user logs in, the login/register text is replaced by a dro ...

Angularjs scope's parent id

I'm looking to access or manipulate a scope's grandparent without making things overly complicated. Leveraging the controller as syntax, I can reference the parent controller like so: this.applicationCtrl.something with applicationCtrl > paren ...

Why doesn't setting the SVG viewBox to "0 0 100 100" scale my path to 100%?

My current dilemma involves an SVG path that is only displaying at 50% of its intended size. How can I adjust it to show at 100%? Below is the code snippet in question: <div className="svgPathContainer"> <svg width="100%" he ...

Click event triggers dynamic function call

Is it possible to have different functions for ng-click depending on the loaded controller page? <a ng-click="removeEvent(event)" class="top_menu_link"> REMOVE</a> For instance, if I want the same button to trigger a remove action but on diff ...

Experiencing Difficulty accessing Methods with Jmeter-Webdriver

var pkg = JavaImporter(org.openqa.selenium) var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait) var wait = new support_ui.WebDriverWait(WDS.browser, 5000) **var support_page=JavaImporter(org.openqa.selenium.WebDriver.Timeouts)** **v ...

Attempting to create a fixed div at a specific position, however, an unexpected Uncaught TypeError disrupted additional code functionality

Is there a way to make a div stay fixed in place while scrolling, but then unstick at a specific point? I found some code that seemed to work for this purpose, but unfortunately it caused an error that affected other jQuery scripts. The console displayed t ...

Utilize the $scope variable as a JSON object for interacting with the $http service

Is there a way to extract all the data from Angular's scope service without creating an additional object? I am trying to send everything on the scope to a server using $http.post, but it seems that $scope is a circular structure, resulting in "Conver ...

Do ES6 features get transpiled into ES5 when utilized in TypeScript?

After implementing ES6 features such as template strings, arrow functions, and destructuring in a TypeScript file, I compile the code to regular JavaScript... Does the TypeScript compiler also compile the ES6 syntax, or do I need to utilize another compil ...