【问题标题】:Not able to access session storage while page routing in reactjsreactjs中的页面路由时无法访问会话存储
【发布时间】:2021-05-01 20:07:10
【问题描述】:

当我在注册提交时路由我的页面时,我会在 sessionStorage 中提交电子邮件。

但是在从会话存储中获取项目时,我无法这样做。提交注册后,我的页面正在路由到另一个页面

import React, { Component } from "react";
import { Link } from "react-router-dom";
import axios from "../axios/axios.js";

export class SignUpRedirect extends Component {
  constructor() {
    super();
    this.state = {
      U_Id: null,
      isLoading: true,
    };
  }

  async componentDidMount() {
    const response = await axios.get("/Organization");
    const idData = response.data;
    idData.forEach((item) => {
      if (item.Email === sessionStorage.getItem("email")) {
        this.setState({ U_Id: item.U_Id, isLoading: false });
      }
    });
  }

  componentWillUnmount() {
    this.setState = (state, callback) => {
      return;
    };
  }

  render() {
    return (
      <div className="donestatus-container">
        You Have Signed Up And Your Organization Id is :{" "}
        {this.state.isLoading ? "...loading" : this.state.U_Id}
        <div className="donestatus-container-inner">
          <Link to="/sign-in">
            <h4>Sign In</h4>
          </Link>
        </div>
      </div>
    );
  }
}

export default SignUpRedirect;

我找不到合适的答案。谁能帮帮我?

【问题讨论】:

    标签: javascript reactjs http session axios


    【解决方案1】:

    试试下面的代码,因为你需要解析 sessionStorage 对象,它就像一个 JSON Onject:

    async componentDidMount() {
      const response = await axios.get("/Organization");
      const idData = response.data;
      idData.forEach((item) => {
        if (item.Email === JSON.parse(sessionStorage.getItem("email"))) {
          this.setState({ U_Id: item.U_Id, isLoading: false });
        }
      });
    }
    

    【讨论】:

    • 仍然没有得到它。实际上我的程序运行正常,但是当我提交我的注册详细信息和页面路由时,它只显示加载......,没有给我结果,但是当我刷新页面时它开始向我显示数据......这意味着它无法获取 sessionStorage 中的值,但刷新后却可以。你能解释一下我哪里做错了吗?
    猜你喜欢
    • 2020-04-12
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2018-03-26
    • 1970-01-01
    • 2017-05-04
    • 2021-03-16
    相关资源
    最近更新 更多