Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

clarg18's avatar

Livewire sharing API results among components

Hello,

I have several livewire components that process data from an API. Rather than have each component call the API, how can I handle it such that they share the same API call.

I thought of passing the API call response in from the controller to the view, but then the page would need to wait for the api response, right?

0 likes
7 replies
webrobert's avatar

@clarg18, generally it's not fun to pass around a lot of data between livewire components.

you can emit it using events or save it in the session. For your waiting question, you can use wire:init to let the page load while waiting for a query to finish. There is also, prefetching which is useful sometimes.

clarg18's avatar

@webrobert Thank you, but wire:init would not help if I perform the API call in the Laravel Controller and pass the the view, correct?

webrobert's avatar

@clarg18, no you'd want to call it from the livewire component. like the example. You have to do the work somewhere. I actually think its one of the cool uses of livewire. The page can load, your api gets triggered. Show a loading message, etc. until the response arrives.

1 like
vincent15000's avatar

@webrobert Ok ... I'm interesting too ... if I have well understood, wire:init can be used if you want to load immediately some part of the page, and defer (via wire:init) the loading of other part of the page.

webrobert's avatar

@vincent15000, as example I just finished a stock chart feature, the page loads, then wire:init orders the chart, after it's ready, I use a browser event to send the data to chart js. This is one component.

clarg18's avatar

@webrobert so you think, one component queries the API and then emits the response as an event for other components to pick up?

webrobert's avatar

@clarg18 You could do that. I don't know what you are doing? Like is said when possible I use fewer components. But if I need say, a guests location (I get it from an api), I save it in session/etc. And emit an event after it's set.

Please or to participate in this conversation.