En la función de actualización (changeProps), tengo algo como esto:
update(changedProps) { if (this.person) { this.__arr = ['hi', 'hi', 'hi']; } else { this.__arr = ['bye', 'bye', 'bye', 'bye']; } if (this.__hello) { this.__arr.splice(1, 0, 'hello'); } super.update(changedProps); }Digamos que this.person es verdad, cuando hago clic en otro lugar y la página vuelve a mostrarse, se vuelve
this.__arr = ['hi', 'hello', 'hi', 'hi'] Luego, cuando se vuelve a representar: this.__arr = ['hi', 'hello','hello', 'hi', 'hi']
Sigue agregando a la matriz después de cada renderización. Lo mismo si esta persona no es verdadera. ¿Cómo hago para que solo lo agregue una vez?
Si hice algo como esto: this.__arr = [...this.__arr, 'hello'] , puede agregarlo al final, pero quiero agregar un elemento al índice 1
Dos cosas:
if (this.__hello) { this.__arr = [...this.__arr].splice(1, 0, 'hello'); } if (this.__hello && this.__arr[1] !== 'hello') { this.__arr = [...this.__arr].splice(1, 0, 'hello'); }