opencrvs / opencrvs-core

A global solution to civil registration
https://www.opencrvs.org
Other
82 stars 62 forks source link

React stack modernization #3978

Open jpye-finch opened 1 year ago

jpye-finch commented 1 year ago

Our frontend is getting fairly complicated - and some parts outdated - given it's written with modern technologies. Some patterns are getting old and are not being learned by new developers. We need to actively continue refactoring the frontend to keep it modern and exciting for developers to use.

Important for being able to use hooks in all of our views.

We have to rethink what we need Redux for. It adds complexity via the action/reducer pattern, which are in their current state difficult to track, especially as the dev tools handle so much data that it crashes. For data fetching, we already have Apollo which does caching, and it's hooks can be directly used in every component. Redux can be a good tool for storing some data; even then, it's recommended to use Redux Toolkit.

I advocate getting into a mindset to avoid state management at all times. Oftentimes what first feels like a new global state is needed, is achieved via the data fetching tool (Apollo/react-query) or URL parameters. If there is a usecase that cannot be solved with those, there are simpler tools like jotai instead of Redux.

https://github.com/opencrvs/opencrvs-core/blob/develop/packages/client/src/views/OfficeHome/inProgress/InProgress.tsx

Problems:

tareq89 commented 1 month ago

For central state management library, can Zustand be a candidate? It is catching up very fast and have hooks like familiar interface.

https://npm-compare.com/@reduxjs/toolkit,zustand,jotai,recoil,valtio/#timeRange=ALL

Zangetsu101 commented 1 month ago

React Router v6

The other thing about search/route params is that we should consider using some runtime validations(e.g. zod) and proper error handling for incorrect values

Zangetsu101 commented 1 month ago

Another thing to consider is how we want to handle the offline data which is crucial for us. Is using a library sufficient for us? Are there any use cases that require a more custom solution perhaps?

naftis commented 1 month ago

For central state management library, can Zustand be a candidate? It is catching up very fast and have hooks like familiar interface.

npm-compare.com/@reduxjs/toolkit,zustand,jotai,recoil,valtio/#timeRange=ALL

Yep it's a good option if we need to build/refactor our centralized state management. Here is a interesting comparison about the differences in the state management tools from Jotai's website: https://jotai.org/docs/basics/comparison

Out of them all I think Recoil is something we should skip as it seems stale compared to zustand/jotai.

naftis commented 1 month ago

Another thing to consider is how we want to handle the offline data which is crucial for us. Is using a library sufficient for us? Are there any use cases that require a more custom solution perhaps?

Definitely..

Could we suffice with Apollo doing most of offline caching? If there's something special (like PIN) we need to add to localStorage we could listen to the changes using Jotai https://jotai.org/docs/utilities/storage or Zustand https://docs.pmnd.rs/zustand/integrations/persisting-store-data

This would require refactors like location tree being fetched from gateway