Search code examples
reactjsreduxdispatch

Retrieved data from Localstorage not fresh


I am pushing data into my local storage using dispatch then I history push to /cart to go further

But the data isn't what've just dispatched, not until I've forced refreshed my page.

How can I always have the most fresh localStorage data with a history.push workflow ?

const submitHandler = (e) => {
    dispatch(
      saveBeneficiaireProfile({
        firstname,
        identifiant,
        adultCount,
        childCount,
        babyCount,
      })
    );
    props.history.push("/cart");
  };

enter image description here


Solution

  • Get fresh data using useEffect hook.

    useEffect(() => {
        
        const profile = localStorage.getBeneficiaireProfile();
        //Also you can save data to state variable and use anywhere
        
      }, []);//this only initialize once 
    

    Retrieve data to local storage

    getBeneficiaireProfile = ()=>{
        //Get data from local store and return
    }