1
 public class test{

    @AuraEnabled
    Public Static Integer init1;

    @AuraEnabled
    Public Static Integer init2;

    @AuraEnabled
    public Static string returnStringOnlyButInitInteger(){
        Init1 = 10;
        Init2 = 20;
        return "I have initialized the Integers";
    }

}

If I have initialized the string variables in the Apex Controller and call the method on Component initialization. How can I access the other variables in Component ? Thanks

1 Answer 1

4

You can't. I think at one point Lightning was meant to have a mechanism where you'd be able to have automatic view state, like in Visualforce, but that hasn't materialized as a feature. The documentation states:

Don’t store component state in your controller (client-side or server-side). Store state in a component’s client-side attributes instead.

You are advised to store data in attributes only, and pass them back and forth as parameters and return values as necessary.

2
  • Thanks a lot..... Is storing a lots of attribute in the lightning component affects the performance a lot ? If yes , by what factor..What are the best practices for this. May 15, 2018 at 3:21
  • @amidstCloud There are performance problems if you use a lot of two-way data bindings (aura:valueChange). There's no real problem with storing a lot of data in a lot of attributes that don't fire events. And even if they do, as long as your component is reasonable, you should be just fine.
    – sfdcfox
    May 15, 2018 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .