Lightning

Using Helper in Lightning Web Component (LWC)

If you have worked on Aura component, you may know how important helper JS file in Lightning Aura Component is. Helper class helps you to reuse a method in any main class. It also helps you to keep main JS file clean.

Salesforce always recommends making best use of helper class. All logic should be written in helper class with proper naming conventions. And main JS file should just call the helper methods.

In this post we will see how we can make use of helper class in Lightning Web Component. I am completely new to this topic I may not give all the information in this post. However, I will give you some basic information.

Let us see basic example of adding 2 numbers using helper class.

HTML File

<template>
    <div class="slds-box slds-theme_default"> 
        <lightning-input type="number" variant="standard" name="Value1" label="value 1" value={value1} onchange={handle_Value1_change} ></lightning-input>
        <lightning-input type="number" variant="standard" name="Value2" label="value 2" value={value2} onchange={handle_Value2_change}></lightning-input>

        <lightning-button variant="brand" label="SUM" title="SUM" onclick={handleClick}></lightning-button>
        {result}
    </div>
</template>

Main JS File.

import { LightningElement } from 'lwc';
import {sayHello, add} from './SampleDemoHelper';

export default class SampleDemo extends LightningElement {

    value1;
    vault2;
    result;

    connectedCallback(){
        console.log('Hello from main SampleDemo');
        sayHello('Akhil Kulkarni');
    }


    handle_Value1_change(event){
        this.value1 = event.target.value;
    }

    
    handle_Value2_change(event){
        this.value2 = event.target.value;
    }


    handleClick(event){

        var sum = add(this.value1, this.value2);
        this.result = "SUM of " + this.value1 + " and " + this.value2 +" is: "+ sum;
    }
}

Helper JS File.

export function sayHello(a_Name){
    console.log('Hello from SampleDemoHelper to: '+a_Name);
}


export function add(val1,val2){
    return parseFloat(val1) + parseFloat(val2);
}

You can create a helper JS file in any name, there are no specifications for that. It is good practice to use helper class with a simple naming just to follow LAC naming convention that is helper.js

For instance, I have created a LWC as SampleDemo then my helper class name will be SampleDemoHelper.js

Please note you can create multiple helper classes and give different names. I have tried above example with just one helper class.

All helper class methods are like individual separate methods. They do not have access to any of the variables which are in main JS or any other JS file.

For instance, as a developer as use this.variable name in main JS file. We do not have access to those variables. Treat this helper as a separate method which has no access to variables outside world. You can pass various parameters write business logic and return the value.

You need to append the key word export to all the functions. This means that you are exposing the
helper method. Exposing is not the only step. You also need to mention which methods you will be using in main JS file.

import {sayHello, add} from './SampleDemoHelper';

This means, we are going to use 2 methods sayHello and add which are defined in SampleDemoHelper file. You can define multiple methods which can be used in other components as well. and you have to import those methods which will be used in current component.

OUTPUT:

Lightning

Renderer in Lightning Bundle

Renderer in Client-side is to override default rendering for a component. You can consider what data should render on load or on reload. The framework’s rendering service takes in-memory component state and creates and manages the DOM elements owned by the component. If you want to modify DOM elements created by the framework for a component, you can modify the DOM elements in the component’s renderer. Otherwise, the framework will override your changes when the component is rerendered.

The base component in the framework is aura:component. Every component extends this base component.

This renderer has base implementations for the four phases of the rendering and rerendering cycles:

  • render()
  • rerender()
  • afterRender()
  • unrender()
Continue reading “Renderer in Lightning Bundle”
Lightning

Helper in Lightning Bundle

Helper in lightning bundle is similar to controller where you write client-side javascript methods. These methods are more like reuseable methods which can be called from any controller method.

  • Helper is similar to controller but helper does not handle events.
  • Helper can be used to write reusable Javascript methods and these can be called from Controller section.
  • Helper functions also enable specialization of tasks, such as processing data and queueing server-side actions.
  • Helper methods are local to Component bundle.
  • Helper methods can be called from any controller javascript methods.
  • Declaring a helper method is similar to controller javascript method.
  • A helper function can pass in any arguments required by the function, such as the component it belongs to, a callback, or any other objects.
  • You can call a helper method from another helper method using this. operation.
Continue reading “Helper in Lightning Bundle”