Best way to organize functions in React?

Hello everyone!

Question: What would be the best way to setup my functions associated to e.g. Mongodb Collection Objects? Are there Best Practices in React, Meteor?

I used to use the package Collection Helpers in blaze, where I could call Mongodb queries insight the method and everything was reactive.

Using this, in autorun, Messages AND Streams was both reactive.

Now in react, I have:

Bildschirmfoto 2024-02-12 um 15.30.15

I keep reactivity through the dep Array, but I could not use helper code like shopCart.getStream() as a inner Query to Collections.Streams.findOne(shopCart.stream_id) would not be reactive.

I found the Collection Helpers package really cool, because I could use code on both client and server side.

Do you know some cool patterns?

Thanks in advance :smiley:

Can you get the results of shopCart.getStream() and assign the results to a React component useState variable? That would make the React component reactive. (I havenā€™t used MongoDB except for Meteor Accounts.)

I have been lately struggling with React and reactivity. The big change/return now is to have components dedicated just to data fetching while previously it used to be in one component or we have HOCs (which are now making comeback). Since my project is almost 10 years old it is a mix, but lately while experimenting with React 19, the older pattern with co-located data fetching has been causing a lot of problems. In React 18 I could get around it with suspense, but that seems no longer be possible, so now Iā€™m hunting for the problem code and separating things. The upside of this is that it will make it easier to use the show components in Storybooks and other tools outside of Meteor.

Though I think we need a guide on the proper React pattern with Meteor. Anyone has any good suggestions here?

Iā€™ve tried working with React Routerā€™s data fetching, and it isnā€™t compatible with having ā€œunknownā€ components on the router level.

With use() and suspense, I am planning to make stand-alone components that have the following structure:

Suspense Boundary > data fetching > UI

Data fetching can be either a subscription or a method.

For our use, the component should be the same for both client and SSR (I havenā€™t thought yet how this will work with RSC).

I still do not know if this will work with React 18 and Meteor 3. We also have a mix of hooks and classes

Many developers are revolting against the ā€œbest practiceā€ of hoisting the data fetching up at the router level instead of near the component where it is used.

1 Like

This is getting super interesting. Overall I have noticed a lot of issues when trying to upgrade to React 19. The upside so far is that it is forcing me more to separate data loading into separate components.

The challenge is that this is one of those cases wherein what is best for the user is bad for developer experience.

In terms of developer experience, itā€™s easier to fetch data nearest to where itā€™s used. Standalone components are reusable and easy to use and understand. Component encapsulation is the goal.

In terms of user experience, the best scenario is to fetch data once before rendering anything (sometimes even before knowing if you will need the data or not). This removes the loaders per component and friendly for slow internet connection. In this case, graphql and router-based loading shines.

For the meantime, React 19 is on hold due to these two opposing patterns
https://x.com/sophiebits/status/1801663976973209620

To add some more abstraction to this, I think data fetching functions can stay nearest to where data is being used. However the function and the method in Meteor can be different things.

As I use Redux in all my projects, Meteor Methods calls can be ā€¦ anywhere on the client. The Redux actions (that invoke Meteor methods) are always within the component. I think it works the same with React Context. It is indeed inefficient to pass subscriptions through a global context and I suppose they best stay at the (parent) components level (without using too much granularity - using too many subscriptions for too many nuclear components)

1 Like