I've tried to create auxiliary classes in the component folder to divide logic into smaller parts, but I can't make the data on the page refresh.
for example:
Example file structure:
component/
|-component.html
|-component.js
|-component.css
|-component.js-meta.xml
|-userData.js
I've created file userData.js
that will contains information about user:
helper class:
export default class UserData{
#firstName;
#lastName;
constructor(firstName, lastName){
this.#firstName = firstName;
this.#lastName = lastName;
}
get lastName(){
return this.#lastName
}
set lastName(value){
if(value.length > 0){
this.#lastName = value
}
}
// rest of the code
}
In the component.js
, I've imported this class and created an object:
Main JavaScript:
import {LightningElement} from 'lwc';
import UserData from './userData';
export default class Component extends LightningElement {
userData = new UserData('firstName', 'lastName');
handleOnChange(){
userData.lastName = 'newLastName';
}
}
And in the component.html
the data was bound:
HTML:
<template>
<p>{userData.firstName} {userData.lastName}</p>
<button onclick={handleOnChange}>Click</button>
</template>
In this situation, after modifying the data in the object, HTML did not refresh the values.
Is there any way to create an auxiliary class that is reactive?