How Can I set OnClick for the Ok button in Alert Box?

Viewed 29

I am Creating A systemtimeout functionality for my MERN App. I tried using React-idle-timer but unfortunately, it did not work. If someone can help me with that then it will be great too.

I tried another way to dislplay an alert box after a certain time. I want to create an OnClick for the OK button in the alert box. it means after the alert box will be displayed when user will click on OK button it should run Logout Function and redirect user to the login page.

Here is the code for useTimeout.jsx and I am calling Alert box in App.js Also I am adding Logout funcion whcih I want to add as Onclick of okay button

useTimeout.jsx

import {useRef, useEffect} from 'react';

export default function useTimeout(callback, delay){
    const stableCallback = useRef(callback);

    useEffect(() =>{
        stableCallback.current = callback;
    }, [callback]);

    useEffect(()=>{
        const tick = () => stableCallback.current();
        if (typeof delay !== 'number') return;
        const t = setTimeout(tick, delay);
        return ()=> clearTimeout(t);
    }, [delay]);

}

This is how I have called it in App.js

import useTimeout from "./components/reactidealtimer/useTimeout";
 
function App() {
  const { user } = useContext(AuthContext);
  useTimeout(()=> alert("Session Timed Out"), 10000);
  return (

Here is the logout Function which I want to add

function  logout(){
    localStorage.clear();
    window.location.href = '/login';
  };
0 Answers
Related