State management best practices
I'm doing a pretty involved estimator. Lots of fields, lots of repeaters, lots of afterStateUpdated to re-calculate after fields have been updated.
The callback in afterStateUpdated tends to be a lot of code. So I'm trying to refactor but state management gets challenging here as I pass data to functions to re-calculate.
What's best practice here?
I've tried stuff like this:
$instance = new self();
And then in afterStateUpdated I can do this:
->afterStateUpdated(function ($state, Forms\Set $set) use ($instance) {
$instance->setTotalsAfterGlassUpdate($state, $set);
}),
And that works but if that if that callback requires other data, I have to pass it. And there's no global state here and I'm not sure a singleton would work. What I was doing, which worked, was using $get so that was acting as my state. But that felt messy and it felt like I was asking for trouble.
Surely others have run into this. Filament has made a wonderful panel to collect all this data but the code in my callbacks was getting huge and I don't see a nice neat way to refactor. What's everyone else doing?
2 Replies
I just use static helpers, and pass in $get, let Filament maintain state.
Yeah I’ve been considering exactly that. Thanks.