tailor-react-spa icon indicating copy to clipboard operation
tailor-react-spa copied to clipboard

How to make the differents fragments communicate together

Open othke opened this issue 7 years ago • 12 comments

Hi,

I would like to know what is the best solution to make those fragments communicate with each other ? If we think to the basic Flux problematic, let says we want that a new message in Chat Fragment display a notification into the Header fragment. I think I would use a state management like Redux but I don't understand how to setup this with Tailor.

Because each React components are mounted independently, I don't understand how could I nested them into a Redux Provider ?

Thanks for your recommendations :smiley:

othke avatar Sep 13 '18 21:09 othke

I also have similar question.How state of one Fragment passed to Another Fragment with React and Redux

waytoharish avatar Sep 20 '18 08:09 waytoharish

You shouldn't be passing full states between fragments. Fragments should know as little as possible about other ones. For communication you use vanilla CustomEvents or some sort of an event bus

shilangyu avatar Sep 24 '18 14:09 shilangyu

Hi Please can you share some example.In Our case we have root app which passing all states to child modules using redux.Another question I want to know can be still use redux to maintain the state at global level??

waytoharish avatar Sep 25 '18 08:09 waytoharish

This is very urgent as we are planning to offer your dream work to the clients

waytoharish avatar Sep 25 '18 08:09 waytoharish

i have done an app using tailor with react-redux fragments (sadly i cant open source it). Heres how it works: Each of your fragment has its own store, it is not shared across other fragments. The idea of microfrontends (fragments) is for them to be as self-sufficient as possible. Let's assume you choose vanilla CustomEvents:

somewhere in header fragment:

// add an event listener
window.addEventListener('addToCart', e => addToCart(e.detail)) // all the sent data will be in the detail property

addToCart here could be an action dispatcher

somewhere in shop fragment:

let sendItemInfo = new CustomEvent('addToCart', {
  detail: {
    id: 123
  }
})
window.dispatchEvent(sendItemInfo)

Above could be a reducer

Remember that once tailor assembles your fragments they all share the window object, meaning you can communicate through it

shilangyu avatar Sep 25 '18 09:09 shilangyu

So we have to emit the event from heder and capture in individual fragment??

waytoharish avatar Sep 25 '18 11:09 waytoharish

Yep, whoever will need emited data can just create a event listener

shilangyu avatar Sep 25 '18 17:09 shilangyu

I changed the code in this repo in order to show how to communicate and react to changes on each Fragment as detailed by @shilangyu in the #11 PR =)

I will probably add an example of server-side rendering with this in mind as well

armand1m avatar Dec 17 '18 11:12 armand1m

Yep, whoever will need emited data can just create a event listener

Let's say, I have created a custom event and made it communicated. Is it secure? If anyone ( Unknow user ) manually triggers a custom event from the console (Chrome debugging console / Snippets) it will spoil the application security, right?

SathishKumar140 avatar Jun 20 '19 10:06 SathishKumar140

it depends on which kind of operations you're relying your events on IMO

armand1m avatar Jun 20 '19 12:06 armand1m

@SathishKumar140 well we are talking about front-end after all, things are exposed to the user and they can be easily intercepted. So yes it's not 'secure' however sensitive information shouldn't be passed anyways, just logic

shilangyu avatar Jun 20 '19 16:06 shilangyu

@SathishKumar140 well we are talking about front-end, after all, things are exposed to the user and they can be easily intercepted. So yes it's not 'secure' however sensitive information shouldn't be passed anyways, just logic

Please refer - https://github.com/zalando/tailor/issues/142#issuecomment-504012768

Basically, all I need to know is there any secured way can communicate between two micro-apps.

SathishKumar140 avatar Jun 20 '19 19:06 SathishKumar140