+ Start a Discussion
kaka kumarkaka kumar 

Why don't we need viewstate in lightning

Hi all ,
I am confused about veiwstate thing in lightnign and why don't we need that in lightning.how do lightning maintain state of variables in page?
You need to maintain more of your view state/page state in the page itself using Javascript. An approach that works well is to grab all your data values and encode them as JSON strings, returning them to the Page as a String property. Then within the page init method, you write Javascript to decode these values and store them in a Javascript array. Similarly when you send data to the server’s JS remoting methods, JSON-encode your data.
i hope it would be help to you ,please make it best answer if it helpfull for you.
Timothy Gentet O'BrienTimothy Gentet O'Brien
I have been unable to find any solid evidence about this yet!

However, just to add to the above; I believe the lack of a viewstate here is due to the compiler needed for a LC being different to the compiler needed for VF pages.

If you think of it this way:
When a VF page is rendered and outputs data it is compiled server side and the HTML is then pushed to the client side and displayed to the user, therefore utilises a LOT of resource to try and compile a page, for example, that has 1,000,000+ rows of data along with styling, etc...

Whereas, if you are using a JS framework, it will utilise the client side resources to actually compile and output the data that is needed... if you were to load the 1,000,000+ rows of data in a LC; it will output the rows, but it will take a while to load and will, potentially, crash the browser that is trying to render the page due to the resources being drained from the client side as opposed to the server side.
Chiragkumar Patel 9Chiragkumar Patel 9
Look this as little more toward behavioural pattern of both the framework . 

VF : Page and Extension/Custom controller is tightly bound , All the Data majorly used in VF page is defined in the Apex Controller With Get/Set methods, every time apex controller method is invoked it synchronizes the all the attribute from Client- Server and vice-versa , Also the method call was sunchronous. To control the latency and performance ViewState was Used. 

Lightning : Client & Server component is loosely coupled - All the attribute are defiend at the client and populated intially and maitained at client side only till you don't explicitly enque the action. This too happens asynchronously and you choose the amount of data that trasits to and fro. 

So now client-server communication is not framework controlled but developer controlled (Bad design / development can make lightning application real patchy and slow)and that is the reason we don't have viewstate.
Chiragkumar Patel 9Chiragkumar Patel 9
Like and Mark as the best answer.