The time function that is stored in the browser's local storage is persisting even after attempting to delete it with window

I have been designing an application that tracks the expiration of food items displayed in a restaurant. The user inputs the food details when it is transferred from the kitchen to the display, triggering a timer based on the intended display duration (1 hour, 2 hours, etc).

I save the time in local storage with a unique identifier linked to each food type. However, I am encountering issues with my code that is supposed to remove the food name from local storage upon expiration.

import React, { useState, useEffect } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import customFetch from "../utility/customFetch";

function TimeFunction({ shelfLife, productId, id, status, expireIn }) {
  // let updatedProductAdded = [];

  const queryClient = useQueryClient();

  const { mutate: updateAddedProduct, isLoading } = useMutation({
    mutationFn: (updatedProductAdded) => {
      console.log(updatedProductAdded);
      return customFetch.patch(`/updateAddedProductOne`, updatedProductAdded);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["addedProducts"] });
      console.log("updated");
    },
    onError: (error) => {
      console.log(error);
    },
  });

  if (isLoading) {
    return <h3>Loading...</h3>;
  }

  ...

Answer №1

Avoid using windows.localStorage and instead directly call localStorage.removeItem("item") in your code, removing the unnecessary "windows" keyword.

localStorage.removeItem("item");

Additionally, remember to verify the name of the item key before removal.

localStorage.removeItem(productId);

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 SocketIO.Server instance mysteriously vanishes upon the installation of socket.io

I'm encountering a strange issue while trying to set up a Socket.io server in nodejs. When I create the io object using const io = require('socket.io')({});, it only returns const io: any without any properties or methods, making it impossib ...

Encountering difficulty locating a module despite following the correct path according to the official NEXT.js documentation

Recently, I delved into the world of next.js and found myself engrossed in chapter 4 of the official documentation titled "Creating Layouts and Pages." In this chapter, I was prompted to create a file named layout.tsx and insert the following code: import ...

Having trouble displaying the output on my console using Node.js

Hey there, I'm new to this community and also new to the world of nodejs technology. I have encountered a problem that may seem minor to you but is quite big for me. Here's what's going on: In my code snippet, I want a user to input 3 value ...

When printing, the CSS for media print is not correctly displaying the table format

I have a basic table with a button underneath. This code snippet is located in the body section of my JSP file: <div id="myDivForPrint"> <table class="t1"> <tbody> <tr> ...

Simple ReactJS code not triggering onClick event

I'm puzzled as to why the event listener isn't firing. I even tried not commenting out the bind, thinking it's unnecessary, but still no luck. It's unclear what the error might be... I've double and triple-checked the last line (r ...

Struggling to retrieve an integer value from user input?

Hey there! I am facing an issue where I receive data from a form via POST, and some fields need to be treated as integers. However, when I use: var_dump($_POST) All the data is returned as strings enclosed in double quotes, which I would like to remove. ...

The unique identifier for a specific child element in Firefox

While working on this website: I customized the right sidebar navigation using: .sidenav a:after { display: block; position: relative; z-index: 0; top: -3.6em; left: 0.15em; width: 90%; content: "."; font-family: 'R ...

Tips for dynamically adjusting an iframe's content size as the browser window is resized

Currently, I am in the process of building a website that will showcase a location on a map (not Google Maps). To achieve this, I have utilized an iframe to contain the map and my goal is for the map to adjust its width based on the width of the browser wi ...

The function findOne() in MongoDB retrieves a value, however, in the node environment, it does not return the value

Having an issue with retrieving values from MongoDB in Node using a function. The data is being found within the findOne function, but it returns undefined when accessed outside of it. Check out the code snippet below: async function findFile(userid) { ...

Centered item in Bootstrap navbar with others aligned to the right

Experimenting with ReactJS and Bootstrap 4 to create a customized NavBar. Seeking guidance on aligning elements in the NavBar, such as centering some and placing others on the right side. Particularly interested in positioning the logout icon on the righ ...

Implementing append operations in JavaScript without relying on the id attribute

Is there a way to specify the second div without assigning it an ID or using any attributes, and then perform an append operation inside it? For instance, indicating that the p element should be appended within the second div without relying on an ID or ...

Struggling to set up a css3 keyframe slide effect and seeking help to properly configure it. Check out the Fiddle for more details

I am looking to create a responsive version of the animation in this fiddle (link provided below). I want the animation to be 100% wide and have a height of 500px. However, when I adjust the width to 100%, it causes issues at the end of the animation. Can ...

Tips for creating a zoomable drawing

I have been working on this javascript and html code but need some assistance in making the rectangle zoomable using mousewheel. Could someone provide guidance? var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var width ...

Navigating to Controller Method Using href Attribute

As I work on my ASP.NET core web app, I encountered an issue with redirecting users to a controller method. The code snippet below failed to work, possibly due to the absence of an href attribute: <a class="btn btn-outline-dark" asp-controller ...

What is the process for fetching a specific image from a database folder by referencing its name?

We have a collection of images stored in the uploads folder within the root directory. Our goal is to display a single image for each user profile. Each user's profile image file name is saved in the database table called user under the field profile ...

Unable to render HTML in Simple Product Name on Magento platform

Is there a way to include HTML in my basic product titles, such as: <strong>A.</strong> Product Title Name However, the frontend displays these HTML tags. Is there a method to enable parsing of all HTML tags? ...

Using Express and GraphQL to implement Windows authentication

Currently, I am working on incorporating Windows Authentication into my React application using apollo client. The backend is powered by apollo graphql server and I have utilized node-sspi to access the logged-in user from Windows. The issue arises with m ...

An application running on Node.js/Express and hosted on Digital Ocean encounters the "Cannot get /" error message

Despite searching extensively online and sifting through numerous solutions to different scenarios, I remained unable to find a fix that resolved my issue. When attempting to run my server.js file locally, everything operates smoothly. However, upon transf ...

Exclude basic authentication for a specific route

Using node, express and connect for a simple app with basic HTTP auth implemented. The code snippet below shows part of the implementation: var express = require('express'), connect = require('connect'); app.configure = function(){ ...

When handling Bootstrap Alerts in React, a common error message may appear: "The node you're trying to remove is not a direct descendant of this

I am trying to simplify my code by creating a universal React component for Bootstrap alerts. Here is my current code: export function combineClassNames(...classNames) { return classNames.filter(c => c).join(' '); } export default functi ...