Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Wireuse Laravel Package

foxws/wireuse

View on GitHub
Deep Wiki
Context7

title: State Objects order: 5 tags:

  • livewire
  • states
  • components

Introduction

State objects are based on the idea of states that you find, for example, in a VueJS Store.

This can be used to make your Livewire component more lightweight, and to separate code. It is also helpful to shared state and passing to sub-components (tabs, wizards, filtering, modals, etc.).

Usage

Create a State class:

namespace App\Posts\States;

use Foxws\WireUse\Support\Livewire\StateObjects\State;
use Livewire\Attributes\Locked;

class PostState extends State
{
    #[Locked]
    public ?string $id = null;

    public function tags(): array
    {
        return $this->getComponent()
            ->post
            ->tags
            ->pluck('name')
            ->toArray();
    }
}

Create a Livewire Component:

use Foxws\WireUse\Views\Support\Page;
use App\Posts\States\PostState;

class PostViewController extends Page
{
    public Post $post;

    public PostState $state;

    public function mount(): void
    {
        $this->state->fill([
            'id' => $this->post->getRouteKey(),
        ]);
    }
}

You can call any state object in your Blade components:

<div class="container">
    <p>{{ $this->state->tags() }}></p>
    <p>{{ $this->state->id }}></p>
</div>

The Foxws\WireUse\States\Concerns\WithState trait can be used to reactively inject a state object into a sub-component:

<livewire:post-edit-details :$state />
use Foxws\WireUse\States\Concerns\WithState;
use Livewire\Component;

/**
 * [@property](https://github.com/property) PostState $state
 */
class PostEditDetails extends Component
{
    use WithState;

    protected function getModel(): ?Post
    {
        return Post::find($this->state->id);
    }
}

Note: At the moment it only works if the parent variable is named or injected as $state.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle