简体   繁体   English

React JS - 未定义的变量

[英]React JS - Undefined variable

I'm trying to modify a simple react app, but I don't know a lot about react.我正在尝试修改一个简单的反应应用程序,但我对反应知之甚少。

I'm trying to set a data-attribute to my iframe. This value should be taken from localStorage.getItem().我正在尝试为我的 iframe 设置一个数据属性。这个值应该取自 localStorage.getItem()。

here is my code:这是我的代码:

import React from 'react';
import createReactClass from 'create-react-class';
import {Col, Row} from "react-bootstrap";




const SamplePage = createReactClass({
  displayName: 'Plugin',
  render() {
  
    if (localStorage.getItem('sessionId')){
        let sessionId = localStorage.getItem('sessionId');
    }
    else{
        let sessionId = "";
    }
    
    return (
        
          <iframe src="http://myurl.x/" frameborder="0" style={{height:"85vh",position:"relative"}} id={sessionId} width="100%" height="100%" scrolling="no"></iframe>
        
    );
  }
});

export default SamplePage;

Unfortunately I get a "sessionId is not defined" error.不幸的是,我收到"sessionId is not defined"错误。

Again, I don't know react and it seems really different to classic js for me.同样,我不知道反应,对我来说它似乎与经典 js 真的不同。

Does anyone knows whats I misunderstood?有谁知道我误解了什么?

if (localStorage.getItem("sessionId")) {
  let sessionId = localStorage.getItem("sessionId");
} else {
  let sessionId = "";
}

let variables are scoped to the block that they're contained in. So the first variable is only usable inside the if , and the second only inside the else . let变量的作用域是它们所在的块。所以第一个变量只能在if内使用,第二个只能在else内使用。 Once you get past the else, there is no sessionId variable, and thus id={sessionId} throws an error.一旦你通过了 else,就没有sessionId变量,因此id={sessionId}会抛出一个错误。

To fix this, change the code to what you originally showed:要解决此问题,请将代码更改为您最初显示的内容:

let sessionId;
if (localStorage.getItem("sessionId")) {
  sessionId = localStorage.getItem("sessionId");
} else {
  sessionId = "";
}

That way, there's a single sessionId variable, who's scope is the entire render function.这样,就有一个 sessionId 变量,即 scope 就是整个渲染 function。

You have declared variable sessionId inside the if and else blocks, hence its scope is limited to the corresponding blocks, whether it is if block or else block.您已经在ifelse块中声明了变量sessionId ,因此它的 scope 仅限于相应的块,无论是if块还是else块。 Your error is due to this code here:您的错误是由于此处的代码造成的:

if (localStorage.getItem('sessionId')){
    let sessionId = localStorage.getItem('sessionId');
}
else {
    let sessionId = "";
}

Instead, you should delcare sessionId variable outside if and else scopes to use it outside them.相反,您应该在ifelse范围之外 delcare sessionId变量以在它们之外使用它。

let sessionId="";
if (localStorage.getItem('sessionId')){
    sessionId = localStorage.getItem('sessionId');
}

This will resolve your error !!!这将解决您的错误!!!

Advice : Please do clear your JS basics before starting ReactJS建议:请在开始之前清除您的 JS 基础知识 ReactJS

You define sessionId in a local scope and access it outside of the scope that is why the error says sessionId is not defined .您在本地 scope 中定义sessionId并在 scope 之外访问它,这就是错误提示sessionId is not defined的原因。 You need to create a state that hold sessionId , and update the sessionId by this.setState您需要创建一个包含sessionIdthis.setState ,并通过 this.setState 更新sessionId

const SamplePage = createReactClass({
  displayName: 'Plugin',
  getInitialState: function() {
    return {sessionId: ""};
  },
  setSessionId: function(sessionId) {
    this.setState({sessionId});
  },
  componentDidMount: function(){

    if (localStorage.getItem('sessionId')){
        const sessionId = localStorage.getItem('sessionId');
        this.setSessionId(sessionId)
    }
    else{
        this.setSessionId("")
    }
  },
  render() {
  
    
    return (
        
          <iframe src="http://myurl.x/" frameborder="0" style={{height:"85vh",position:"relative"}} id={this.state.sessionId} width="100%" height="100%" scrolling="no"></iframe>
        
    );
  }
});

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

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