Is it possible to work with static data in Livewire?

When I’m running the code below, everytime I click at the button the rendered data gets overridden.
Is it possible to add new items, without to lose the previous added data at the rerender?

With every click a new item should be added to create a list.

class Test extends Component
    {
        public $items = [];
        public $key = 0;

        public function add()
        {
            $this->key++;
            $this->items = Arr::add(['name' => 'item'], 'key', $this->key);
        }
    	
        public function render()
        {
            return view('livewire.ui.test');
        }
    }

blade:

<div>
    @foreach($items as $item)
        <div>{{ $item }}</div>
    @endforeach

    <button wire:click="add">test</button>
</div>

Are you wanting to add new items to your array? Data should be persisting as indicated by incrementing $this->key. Does that progress as expected? If you dump out {{ $key }} into your blade view, you’ll see that every time you hit add(), it will increase. Data is persisting.

You can add more items to you array like this:

$this->items[$this->key] = $value

or

$this->items[] = $value

What’s happening is that by using Arr::add, you’re just using the same initial array: ['name' => 'item'] and adding another item to that array so your array is likely:

[
    'name' => 'item',
    'key' => '1'
]

If you really want to stick with using Arr::add, it would be:

$this->items = Arr::add($this->items, 'key', $this->key);