Client side JavaScript can be attached to the lifecycle of a component with an f:attribute named clientPreConstruct, clientPostConstruct, clientPostRefresh or clientPreDestroy: before and after it is created, after it is refreshed by an Ajax update and before it is destroyed. clientPreConstruct and clientPostConstruct are called on an Ajax update as well, their refresh parameter tells which. Here each event of the input shows a message: Submit updates the form, Destroy destroys the client side component.
<div class="card">
<h:form id="form">
<p:growl ref="msgs" />
<div class="formgrid grid p-fluid">
<div class="field col-12 md:col-6">
<p:outputLabel for="name" value="Name"/>
<p:inputText id="name" value="#{lifecycleView.text}" ref="name">
<f:attribute name="clientPreConstruct" value="PF('msgs').renderMessage({severity: 'info', summary: cfg.id + (refresh ? ' Component Refreshing' : ' Component Initialized')})"/>
<f:attribute name="clientPostConstruct" value="if (!refresh) PF('msgs').renderMessage({severity: 'info', summary: component.id + ' Component Created'})"/>
<f:attribute name="clientPostRefresh" value="PF('msgs').renderMessage({severity: 'info', summary: component.id + ' Component Refreshed'})"/>
<f:attribute name="clientPreDestroy" value="PF('msgs').renderMessage({severity: 'info', summary: component.id + ' Component Destroyed'})"/>
</p:inputText>
</div>
</div>
<p:commandButton value="Submit" update="@form" icon="pi pi-check" styleClass="mr-2" />
<p:commandButton value="Destroy" icon="pi pi-times" onclick="PF('name').destroy(); return false" styleClass="p-button-danger" />
</h:form>
</div>