Expose Alpine.data and Alpine.store objects #3223
Replies: 2 comments 4 replies
-
Well, Alpine.data does not instantiate a component, so it has nothing to return. Multiple components can use the same data and they get a unique context (that's why the data accepts a function that returns an object. Alpine.store, after instantiation, if you only provide the key, it will return the reactive object you assigned before. This allows you to access it outside of the Alpine context. It's a bit hacky, but, within a data object, you can assign it's context to a variable that is scoped outside of the component like If all you want is a reactive object, you can use But mainly, it's normal in Alpine to do your listener for auth state changed INSIDE the relevant component. You could put it inside the Stores Like in Preact you do that code mostly inside the components, it's the same with Alpine. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hello!
I'm transitioning from @preact/signals and preact world and having a hard time connecting alpine to external API, a bit puzzled with one moment.
I would expect
Alpine.store('someStore', init)
andAlpine.data('someData', init)
to return some proxy object as result, to easily get access to component and to be able externally modify its state. Sort ofThat's can be naturally done with preact-signals or similar observable tool.
I wonder if that's something alpine developers intentionally went against?
What's the most expected way for an app to react on external API callbacks or update its state from outside? Passing changes through firing events on window seems cumbersome.
For now the only way I can see is hoisting API manually in init method:
I had to come up with alternative implementation in sprae
Beta Was this translation helpful? Give feedback.
All reactions