zouhir / preact-habitat

Zero configuration Preact widgets renderer in any host DOM
MIT License
518 stars 43 forks source link

[Question] - Sharing logic between widgets #47

Closed Matth10 closed 2 years ago

Matth10 commented 4 years ago

Hi, I'm trying to build some e-commerce widgets. So I have a add to cart button, a button to open the cart and a cart. I need to share some logic between them. I was wondering how I can do that with preact-habitat ?

Thanks

MykalMachon commented 4 years ago

Hey @Matth10, I Don't know if you're still looking to an answer for this, but I've basically built my own solution for this. I have one app emit a mixture of custom events and writing to local storage for bigger more persistent data components.

From there I just have Preact.js listen for events and read local storage. I don't know if this is the best solution but as a rule of thumb, it's been working for us!

zouhir commented 2 years ago

I am sorry I have not looked at the project in so long; for this question, I think I'd solve it via a functional event emitter library like Mitte https://github.com/developit/mitt

You can have N widgets on the same page communicating with each other via events.