Replies: 5 comments 5 replies
-
Great point 👍 if there's potential performance impact, we should get away from Way 1. When I tried to do Way 1 the point was to reduce business logic from components as much as possible, but it shouldn't compromise the performance. And we have to be consistent on this contract. Just a thought from one joined later without background/frontend-architecture knowledge 😅 |
Beta Was this translation helpful? Give feedback.
-
🤔 TIL I learned that The issue with the re-rendering I think is more related to how we are referencing stores directly from the components, where we probably shouldn't be.
It could just be I'm old-school but to me this pattern doesn't make sense. IMHO, the store should not have any knowledge of the business logic or call services. This will become even more evident if we ever decide to support external stores and we will end up with a lot of duplicated business logic. The longer term goal we had was to make the UI components completely dumb and as thin as possible, with no business logic in them, leaving that to the services we call. If we had another abstraction for something like a more clearly defined domain model, then I see no problem to move hooks or whatever you'd prefer there instead. We can have business-logic aware components separate from presentational components, perhaps that could be a compromise. Having said that I can see where we might not be so strict where there are performance concerns, for example, maybe with the code editor or other places, and re-assess the situation if that becomes a problem in other places too. But maybe I have missed some other key affected areas that should be looked at. |
Beta Was this translation helpful? Give feedback.
-
maybe
which pattern 😃 ?
That's fine, that logic will live in Services
This is also fine, instead of calling a service to make calculations, we could call them from a container component and pass it to the store there, that wouldn't be an issue either. The point is mostly to convey that currently, we have components calling a store, then the store calls a service, then this service calls another store or sometimes even the same one. Other times, a component calls both the store and the service and during these calls, more communication between the store and the service happens as well and that doesn't seem about right 🤔
Yes, absolutely.
The most affected part I would say is everything related to steps, being |
Beta Was this translation helpful? Give feedback.
-
Closing the discussion as an agreement was reached ✌️ Summary by @igarashitm :
|
Beta Was this translation helpful? Give feedback.
-
Context
While working on multiple flows support, I noticed that we're using a few different ways to interact with the stores and that there's no clear (at least from my very friendly-and-humble-point-of-view 😄 ).
Way 1: (https://github.com/pmndrs/zustand#fetching-everything)
From the components, we bind the store completely by using:
While this might be convenient as we get the state updated and also have access to the store API, it has the drawback of re-rendering the component even when unrelated properties change. Sometimes this is not a big problem by itself, it's very noticeable for instance in the Code Editor by the flickering that it generates.
Way 2: (https://github.com/pmndrs/zustand#selecting-multiple-state-slices)
This is a little bit better since now the component will be rendered only when said properties change but we should also combine it with the
shallow
function fromzustand
:Way 3:
Interacting with the store through a service
While this work, it doesn't seem like a common approach to handle store interactions in React, since usually this is done by leveraging hooks
Proposal
Example
Let's take a look into the
VisualizationStep.tsx
component and the mechanism to append a new stepNotice how the
onMiniCatalogClickAppend
handler uses thestepService
to add a new step. In turn, thestepService
leverage theuseFlowsStore
to execute the action.Using the previously described proposal, this should look like
Then, in the
insertStep
function from the store, we would call all the related method to calculate where exactly thisstep
should be included and under which conditions.Beta Was this translation helpful? Give feedback.
All reactions