Ahmed Ibrahim
Ahmed Ibrahim

Reputation: 517

How to disappear alert after 5 seconds in React JS?

import { useState } from 'react'
    
    const Message = ({ variant, children }) => {
      const [timeOut, setTimeOut] = useState(null)
    
      setTimeout(() => {
        setTimeOut(1)
      }, 3000)
    
      return (
        timeOut !== 1 && <div className={`alert alert-${variant}`}>{children}</div>
      )
    }
    
    Message.defaultPros = {
      variant: 'info',
    }
    
    export default Message

I want to disappear this alert after 2 or 3 seconds. I used this logic it's fine and working but In my console, I'm having this warning:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

Is it affecting my app or is it oka? You can give me a better idea to implement this logic.

Upvotes: 10

Views: 64554

Answers (4)

Sankha Rathnayake
Sankha Rathnayake

Reputation: 843

If you use MUI, then you can make alerts appear and disapper with fade in transitions.
Here's the code

import Alert from "@mui/material/Alert";
import AlertTitle from "@mui/material/AlertTitle";
import Fade from "@mui/material/Fade";

export default function Registration() {

  const [alertVisibility, setAlertVisibility] = useState(false);

  return(
    <Fade
       in={alertVisibility} //Write the needed condition here to make it appear
       timeout={{ enter: 1000, exit: 1000 }} //Edit these two values to change the duration of transition when the element is getting appeared and disappeard
       addEndListener={() => {
         setTimeout(() => {
           setAlertVisibility(true)
         }, 2000);
       }}
       >
       <Alert severity="success" variant="standard" className="alert">
          <AlertTitle>Success</AlertTitle>
             Registration Successful!
          </Alert>
    </Fade>
  )
}

For more transitions refer this -> https://mui.com/material-ui/transitions/

Upvotes: 3

Kaung Khant Zaw
Kaung Khant Zaw

Reputation: 1638

import React, { useEffect, useState } from 'react';

const Message = ({ variant, children }) => {

  const [alert, setAlert] = useState(true);
      
  useEffect(() => {
    const timer = setTimeout(() => {
      setAlert(false);
    }, 3000);
    
    // To clear or cancel a timer, you call the clearTimeout(); method, 
    // passing in the timer object that you created into clearTimeout().
    
    return () => clearTimeout(timer);
  }, []);     
    
  return (
    {alert && <div className={`alert alert-${variant}`}>{children}</div>}
  )
}

Upvotes: 1

rotimi-best
rotimi-best

Reputation: 1922

You can read through the comments

import { useState, useEffect } from 'react'
    
const Message = ({ variant, children }) => {
  const [show, setShow] = useState(true)

  // On componentDidMount set the timer
  useEffect(() => {
    const timeId = setTimeout(() => {
      // After 3 seconds set the show value to false
      setShow(false)
    }, 3000)

    return () => {
      clearTimeout(timeId)
    }
  }, []);

  // If show is false the component will return null and stop here
  if (!show) {
    return null;
  }

  // If show is true this will be returned
  return (
    <div className={`alert alert-${variant}`}>
      {children}
    </div>
  )
}

Message.defaultPros = {
  variant: 'info',
}

export default Message;

Upvotes: 21

VersifiXion
VersifiXion

Reputation: 2292

This will show the alert during 3 seconds, then it will disappear :

import React, { useEffect, useState } from 'react';

const Message = ({ variant, children }) => {
  // the alert is displayed by default
  const [alert, setAlert] = useState(true);
      
  useEffect(() => {
    // when the component is mounted, the alert is displayed for 3 seconds
    setTimeout(() => {
      setAlert(false);
    }, 3000);
  }, []);     
    
  return (
    {alert && <div className={`alert alert-${variant}`}>{children}</div>}
  )
}

Upvotes: 4

Related Questions