typescriptasynchronouscallchain

Typescript Chain Async Call


I need to:

localStorage.setItem('user', user); router.navigate(['/profile']);

The problem is that navigation may occur before storage is set.

Please tell me I don't need to route parameters, create asynchronous classes, callbacks, promisses, or timers for this to work.


Solution

  • You may set localstorage value in useEffect hook like this:

     React.useEffect(()=>{ 
       setStorage()
       router.navigate(['/profile'])      
     },[])
     
     const setStorage = ()=>{  
        localStorage.setItem("user", user)  
     }
    

    It'll set the value in localstorage before router.naviagte