Sessionstorage Returning NULL when starting Application

Asked

Viewed 77 times

0

I have a function that is linked to a button, which saves a value in sessionStorage.setItem, and redirects to another page.

Arquivo cliente.js

    pegaValorConta(id) {
     sessionStorage.setItem('id-cliente-conta', id);
     window.location.href = 'http://192.168.0.109:3000/conta';
  }

and I want to show what’s inside sessionStorage on the page conta.js

Arquivo conta.js

      componentWillMount() {

         console.log("Session: ",sessionStorage.getItem('id-cliente-conta'));
    }

The problem is that the first time I start the application and run the function above, the sessionStorage returns null, she only picks up the value when I go back to the archive cliente.js and run the function again.

How do I make sessionStorage do not return the first time the value null?

  • added the way you did, it waits for two seconds to redirect, but keeps returning null the first time. @sam

  • did what you said too, actually I had done before, and it returns the value I expected in id, the problem is in sessionStorage same.

  • I switched to localStorage, but it has the same problem. I will test sessionStorage out of a React application.

  • @sam tested sessionStorage on a pure html page and js, and it worked normal, so I have figured out how to do it in React, plus I’ve already used the componentDidMount function that is used when you want to load something after redenrizing to the page, and yet it still returns null the first time too

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.