简体   繁体   English

如何在不使用全局变量的情况下使此函数工作

[英]How can I make this function work without using a global variable on react

I have an array of data which is divided by 2 groups, I render the description of each element and add a title to each group.我有一个由 2 组划分的数据数组,我呈现每个元素的描述并为每个组添加一个标题。 the thing is I can only do that with a global variable, which is not good.问题是我只能用全局变量来做到这一点,这不好。 how can I solve this?我该如何解决这个问题?

I already tried using let and setting a state.我已经尝试过使用 let 并设置状态。 I need this in react我需要这个来反应

//this is my global var
   var lastGroup;

//this is where I map the data and show it 

{this.state.settings.map((setting, idx) => (
            <div>
   ///here I call the getTitle function that add title to each group
                {this.getTitle( setting.alert_group)}
              <div>
                {this.state.showDescriptiveTitle=true}
                  <div className="settings">
                    <div className="alertType">{setting.description}</div> 
                    <div>
                      <FormGroup>
                        <Switch
                        checked={setting.status}
                        id={idx} 
                        onChange={() => this.onChange(setting, idx)}
                        color="primary"/>
                      </FormGroup>
                    </div>
                  </div>
              </div>

          </div>

//here is my getTitle function that uses the global variable

getTitle( alert_group){

if(lastGroup !== alert_group){
  lastGroup=alert_group;
  if(alert_group=="D"){
    return( <h6 >Descriptive</h6>);
  }else{
    return(<h6 >Predictive</h6>)
  }    
}
}

This works just fine, but how can achieve this without using a global variable?这工作得很好,但是如何在不使用全局变量的情况下实现这一目标?

It's not super clear what you're trying to achieve by not using a global variable, nor what didn't work about using let and state .通过不使用全局变量来尝试实现什么并不是非常清楚,也不是使用letstate不起作用的地方。 These are the options I can think of:这些是我能想到的选项:

  • Use const or let instead of var , which will be block-scoped rather than global.使用constlet而不是var ,这将是块范围的而不是全局的。 In this case you'd have to be in the right scope to access it.在这种情况下,您必须在正确的范围内才能访问它。
  • Define lastGroup as a state property and use setState() to update it in getTitle() , and access it via this.state.lastGroup.lastGroup定义为状态属性并使用setState()getTitle()更新它,并通过 this.state.lastGroup 访问它。 I suspect this didn't work because you're running getTitle() in the render, which would cause an infinite loop (state change causes re-render, re-render causes state change).我怀疑这不起作用,因为您在渲染中运行getTitle() ,这会导致无限循环(状态更改导致重新渲染,重新渲染导致状态更改)。 You would have to figure out some trigger for running getTitle() other than just on every render.您必须找出一些触发器来运行getTitle()而不仅仅是在每次渲染上。
  • If your React component is a class, you could define it as a class property (eg in the constructor create this.lastGroup ) and access it via this.lastGroup如果你的 React 组件是一个类,你可以将它定义为一个类属性(例如在构造函数中 create this.lastGroup )并通过this.lastGroup访问它

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

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