React button remains inactive

After only four months of learning and developing in react, I decided to create a simple portfolio for myself. However, I encountered an issue with a toggler and a button that I included in the navbar - they simply won't respond when clicked no matter what I do. I have provided some code snippets for both the button and the toggler below. Note: The toggler serves as a switch between dark and light themes, while the button is meant to download my resume (the files are not uploaded yet).

Check out the code snippet:

function App() {
  const style =
    "block max-w-sm p-6 bg-white-50 border border-gray-300 rounded-lg shadow-md dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700 w-50 sm:w-50";

  const [mode, setMode] = useState("light");

  const [checked, setChecked] = useState(false);

  function handleCheck() {
    setChecked(!checked);
  }

  const toggleDarkMode = () => {
    setMode(mode === "light" ? "dark" : "light");
  };

  return (
    <div className={mode === "dark" ? "dark" : ""}>
      <ScrollContainer>
        <section className="h-screen w-screen bg-orange-50 dark:bg-gray-900">
          ...
        </section>
      </ScrollContainer>
      <ToastContainer />
    </div>
  );
}
export default App;

I thought I would be able to toggle the button or at least get it to change visually upon hover, but unfortunately, nothing seems to work.

Answer №1

It seems like you've implemented dark mode styling with tailwindcss. To ensure it functions correctly, remember to add this code snippet to your tailwind.config.js:

module.exports = {
  darkMode: 'class',
  // ...
}

For more information, check out this resource.

Answer №2

Apologies, the code structure you provided makes it difficult to follow without a portion of the navbar included.

Based on my intuition, here's an attempt to address your issue:

It seems that your click event is set on a div, but your label is placed outside of it.

It's possible that when you click on something, you're not actually triggering the intended event. Have you considered placing your label inside the click event?

Here are some additional points to consider:

  • You've created two states in your app to manage the theme: checked and mode. This may impact performance as your component will render twice. You could optimize this by using a single state to designate the app as either dark or light.
  • Since your portfolio serves as a public-facing website, adhering to Semantic HTML is crucial. It's recommended to use clickable elements like buttons and href for accessibility purposes. While other elements can trigger events, maintaining semantic HTML is best practice.
  • Your unordered list is disrupted by placing a label inside without wrapping them in li tags.

Please confirm if these suggestions resolve the issues mentioned above. Thank you.

Answer №3

After reviewing your code snippet, it appears that the button lacks an onClick handler, preventing it from functioning when clicked.

Furthermore, nesting interactive elements is discouraged. If you want a link to resemble a button, consider removing the outer button element and styling the a element to mimic a button (as you are currently doing).

To control the cursor, add the cursor-pointer tailwind utility to your classes.

Your dark mode toggle may not be working as expected because the toggleDarkMode method captures the initial value of mode and does not update. To address this, consider using useCallback to ensure the method updates:

const toggleDarkMode = useCallback(() => {
  setMode(mode === "light" ? "dark" : "light");
}, [mode]);

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

Executing a function from another reducer using React and redux

My application consists of two main components: the Market and the Status. The Status component manages the user's money, while the Market component contains buttons for purchasing items. My goal is to decrement the user's money when a button in ...

Creating a full-width menu with Material UI - a step-by-step guide

I am currently working on creating a mobile menu that spans the full width of the screen. Despite my efforts, it seems like I might be misunderstanding something as my CSS rules are not being applied correctly. Here is a live code example for reference: ...

Utilizing the useRef hook in React to retrieve elements for seamless page transitions with react-scroll

I've been working on creating a single-page website with a customized navbar using React instead of native JavaScript, similar to the example I found in this reference. My current setup includes a NavBar.tsx and App.tsx. The NavBar.tsx file consists o ...

Material UI button component does not have the applied class

I'm encountering an issue with applying a CSS class (redirectButton) to a Material UI button. Despite my efforts, the class is not being successfully applied. Below is the HTML code in question: {(isDataAdapterAdmin && !dataSources?.length) & ...

Unable to create a Next 13 application using react-quill

My NextJS app includes the react-quill library for rich text editing. Previously, I had no issues with importing the library dynamically to avoid server-side rendering problems. However, after upgrading to Next 13, I encountered a problem. During the bui ...

Issue encountered: React build fails to load without including index.html in URL during deployment on AWS S3 and Cloudfront

Currently, I am working on a React JS application that utilizes Hash Routing. Everything works perfectly fine in the local environment. For example, when I try to access , the URL does not work as expected. Instead, I have to use for it to function corre ...

Caution: React is unable to identify the `PaperComponent` prop on a DOM element

Trying to create a draggable modal using Material UI's 'Modal' component. I want users to be able to move the modal around by dragging it, so I decided to use 'Draggable' from react-draggable library. However, I encountered this er ...

Material UI's Paper component is not expanding to full width when viewed on a smaller screen size

I'm currently working on a website project and utilizing a component for displaying dark mode. However, I've encountered an issue where the Component shrinks excessively when the screen size goes below 600px, unlike other components. This is a s ...

I am interested in dynamically rendering the page on Next.js based on certain conditions

In my -app.js file, I have the code snippet below: import { useState, useEffect } from "react"; import PropTypes from "prop-types"; ... export default function MyApp(props) { const { Component, pageProps } = props; co ...

Updating of the changed value in the Text Field is not being reflected when submitting in React Hook Form and React JS

I am a beginner with React and encountering difficulties while attempting to handle form submission. If anyone has insights into this issue, please share. I am using React Material-UI, as well as React-Hook-Form with the controller feature. Within my pro ...

Setting up a React Package by reinstalling a git repository

While immersing myself in learning React JS, I decided to create a git repository containing React components that could be exported and used or installed as a package in a separate React application. I developed some UI Components in a git repository and ...

Controlling the access to simpleJWT tokens in Django-React authentication to restrict user permissions

My backend has two React frontends: one for general users and the other specifically for admin users with the is_staff attribute. I have customized TokenObtainPairSerializer to add extra fields like 'is_staff'. Now, I'm considering how to r ...

Trouble with MUI breakpoints interacting with a collapsible drawer

Having an issue with my drawer that I modified to have a default width of 450px and a max width of 800px. The xs value is set to 6 and sm to 4, but it doesn't change as expected. It remains at 4 regardless of whether I expand it to 800px or keep it at ...

Tips on confirming my input field using a specific formula

I am in the process of validating my input field within a redux form using a specific formula. This particular formula consists of 4 key terms that I want to limit users to entering exclusively. The terms are: TERM1, TERM2, TERM3, and TERM4. While constr ...

What is the best way to configure a Next.js API route to make a fetch request to an ASP.NET Core API without encountering issues with self-signed certificates?

I am currently in the process of developing a nextjs app that utilizes a .NET core api as its backend. To handle server-side authentication data and obfuscate endpoints, I am using nextjs' built-in api routes as a proxy to the backend. However, I hav ...

Every time I hit the refresh button, I find myself forcefully logged out

After switching from using localStorage to cookies in my React JS web app, I am experiencing an issue where I get logged out whenever I refresh the page. Even though the cookies are still stored in the browser, the authentication process seems to be failin ...

Issues with React Material UI Select functionality not performing as expected

I've been working on populating a Select Component from the Material UI library in React, but I'm facing an issue where I can't choose any of the options once they are populated. Below is my code snippet: import React, { useState, useEffect ...

After downloading the latest version of NodeJS, why am I seeing this error when trying to create a new React app using npx?

After updating to a newer version of NodeJS, I attempted to create a new React app using the command npx create-react-app my-app. However, I encountered the following error message: Try the new cross-platform PowerShell https://aka.ms/pscore6 PS E:\A ...

React component is experiencing compatibility issues with the MaterialUI component

Attempting to use MUI pre-made components in my React application, I have imported everything correctly. However, when running the app, only the elements appear in the browser without their styles. import React, { Component } from 'react'; impor ...

Encountering an issue with an unprocessable entity message while working with React.js

When I try to fetch a request from the backend, I am getting an error like 'unprocessable entity' even though I have entered the correct 10-digit mobile number. I don't know what I am doing wrong, please help. Please check out: https://ibb ...