I have a LiveComponent with an array of prices, depending on if a checkbox is checked or not the prices should be with VAT in- or excluded.
This works nicely. The problem is:
- When I check the box -> the prices are updated with VAT included (as expected).
- I refresh the page
- The box is still checked (as expected) -> but the prices are not with VAT anymore.
So the LiveComponent saves the state of the checkbox, but does not trigger the correct action after refresh depending on the state.
I thought I could fix it by using #[PreReRender].
<?php
#[AsLiveComponent]
class MyLiveComponent
{
use DefaultActionTrait;
use ComponentWithFormTrait;
use ComponentToolsTrait;
public function __construct(
)
{
}
#[LiveProp(hydrateWith: 'hydratePricings', dehydrateWith: 'dehydratePricings')]
public array $pricings = [];
#[LiveProp]
public bool $inclVat = false;
#[LiveAction]
public function toggleInclVat(#[LiveArg] bool $checked): void
{
$this->inclVat = !$checked;
if ($this->inclVat) {
$this->setPricesInclVat();
}
}
#[PreReRender(-1)]
public function setVatPrices(): void
{
if ($this->inclVat) {
$this->setPricesInclVat();
}
}
private function setPricesInclVat(): void
{
foreach ($this->pricings as $pricing) {
$pricing = $pricing * (1 + 0.21));
}
}
}
The function setVatPrices() is triggered but does not update the prices. Maybe because $this->prices is not set before render?