简体   繁体   English

在条件语句中使用 session 存储

[英]Using session storage in a conditional statement

In my React app's session storage websiteData object, I the following values that I am trying to access:在我的 React 应用程序的 session 存储websiteData数据 object 中,我尝试访问以下值:

websiteData:
conf: type1,
 configuration: 
    linkBackSite: "https://www.webste.com/somePath/"

I created a function that should first looks for the session storage item conf and if that item matches, return the URL string from configuration.我创建了一个 function,它应该首先查找 session 存储项conf ,如果该项匹配,则从配置中返回 URL 字符串。

After that I pass the function into my button's onClick event which would return the user to the relative URL:之后,我将 function 传递到我的按钮的 onClick 事件中,这会将用户返回到相关的 URL:

  const returnToEnv = () => {
    const getConfiguration = sessionStorage.getItem('conf');
    console.log('getConfiguration', getConfiguration);
    if (getConfiguration === 'type1') {
      return JSON.parse(
        sessionStorage.getItem('configuration', 'linkBackSite')
      );
    } else {
      console.log('no');
    }
  };


      <button className="active" onClick={returnToEnv}>
        {i18next.t('CONFIRMATION.return')}
      </button>

When I click the button, nothing is happening and the console log is returning null.当我单击按钮时,什么都没有发生,控制台日志正在返回 null。 What is the correct way to access these items inside of the sessionStorage object and use the returned object in the onClick event?在 sessionStorage object 中访问这些项目并在onClick事件中使用返回的 object 的正确方法是什么?

The solution was easier once I understood the configuration:一旦我了解了配置,解决方案就更容易了:

  const websiteData = JSON.parse(
    sessionStorage.getItem('websiteData')
  );

  const linkBackSite = websiteData.configuration.linkBackSite;

      <a href={linkBackSite} alt="">
          {i18next.t('CONFIRMATION.return')}
        </a>

After calling the main object in const websiteData , I returned the specific key in const linkBackSite .const websiteData中调用主要的 object 后,我返回了const linkBackSite中的特定键。

Since it is pointing to a preconfigured public URL, I removed the onClick event and put it directly in the HTML anchor element.由于它指向预配置的公共 URL,因此我删除了onClick事件并将其直接放入 HTML 锚元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM