Solution

To get data from sessionstorage in react js, use sessionStorage.getItem() method it will return stored value from sessionstorage.

sessionStorage.getItem()

The getItem() method of the Storage interface, when passed a key name, will return that key’s value, or null if the key does not exist, in the given Storage object.

Snippet

In this snippet, we will get some value from localstorage using sessionStorage.getItem() method.

const token = sessionStorage.getItem("access_token");

console.log(token); // dXNlckBleGFtcGxlLmNvbTpzZWNyZXQ=

Example

In this example, we will get token from sessionstorage and show in the console and page.

Let’s start coding…

import React, { useEffect, useState } from "react";
export default function App() {
  const [token, setToken] = useState("");

  useEffect(() => {
    sessionStorage.setItem("access_token", "dXNlckBleGFtcGxlLmNvbTpzZWNyZXQ");

    const token = sessionStorage.getItem("access_token");
    console.log("token", token);
    setToken(token);
  }, []);

  return (
    <div className="App">
      <h1>{`Token`}</h1>
      <p>{token}</p>
    </div>
  );
}

Output

sessionStorage, getItem

codesandbox