9

I have read in the Lightning Component Developer guide that it is recommended to use the component.isValid() check any time you are referencing a component in asynchronous code such as in the helper controller when handling the response from the server-side controller. I have not been able to find in the documentation exactly what this function is doing. Could someone explain what this isValid() function is doing?

Thank you for any information you can provide.

This is an example from the developer guide of how it is used:

({
  getExpenses: function (component) {
    var action = component.get("c.getExpenses");
    var self = this;
    action.setCallback(this, function(response) {
       var state = response.getState();
       if (***component.isValid()*** && state === "SUCCESS") {
           component.set("v.expenses", response.getReturnValue());
           self.updateTotal(component);
       }
});
$A.enqueueAction(action);
},
10

If in the time it took to get the expenses from the server, you switched screens and have unrendered the component that was making the request, the component would have been destroyed.

In this case, you'll still have a reference to that component, but it will no longer be valid.

Simply, checks if the component has been destroyed or not.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.