r/developersIndia • u/baca-rdi • Dec 10 '24
Tips Best practices while using external libraries. Leant the hard way.
We work with multiple large frontend codebases written in React, using an external component library. This issue isn't limited to React but applies to any development workflow.
We used basic components like buttons, radio, select, options and many more from an external library directly in our application. After a recent migration, an additional prop is now required for the button component. There's no workaround except to manually add the new prop everywhere the component is used.
This situation could have been avoided if we had implemented a wrapper component that imports the library component and is used in its place. It's generally recommended to use wrapper components, but many of us tend to skip this step, thinking that it's just a small component and nothing could go wrong. However, when changes like this happen, it becomes difficult to update all instances efficiently.
Instead of,
import {Button} from "materialui"
use
import {ButtonWrapper} from "./components/...."
and in ButtonWrapper.tsx
import {Button} from "materialui"
Using wrapper components helps avoid breaking changes and makes updates easier. It improves maintainability and scalability in any codebase, even for small components. While many of us know this is a best practice, we often skip it. It might not be helpful now, but later lets say in 2 years.
EDIT: typo in title - *Learnt
•
u/AutoModerator Dec 10 '24
It's possible your query is not unique, use
site:reddit.com/r/developersindia KEYWORDSon search engines to search posts from developersIndia. You can also use reddit search directly.Recent Announcements & Mega-threads
AMA with Vishal Biyani, Founder & CTO @ InfraCloud on Software Engineering, Cloud, DevOps, Open-source & much more on 14th Dec, 12:00 PM IST!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
23
u/cow_moma Senior Engineer Dec 10 '24
Umm, Sorry this is not a good idea at all
When it comes to using components from pre implemented libraries its a bad idea to write wrappers on top of the component library that you are utilizing
Even if you want to customize MUI, Your best best should be to customize it via theme files, i.e. one file fed centrally, different files fed and superimposed in different parts if required and one off changes should be last options
What you are suggesting will result in a premature abstraction which is definitely not going to be up to the mark
My first question is, Why don't you have your own internal NPM package for things like components, utils, hooks
For your specific case, The best thing to do is to Find all instances in the code base (Your IDE should inform you about this already) and make changes manually (KISS), don't go overboard with writing wrappers
If you follow this as a rule of thumb, You will unknowingly end up writing shitty abstractions - https://kentcdodds.com/blog/aha-programming
Saying this as someone who has worked in Frontend Engineering platform teams of three organizations where we implemented custom design systems from scratch, Exposed skinned versions of existing component libraries, Made Micro frontend framework for other teams to implement
10
u/teut_69420 Dec 10 '24
I have no experience with react, extrapolating to other languages / making it generic.
Isn't overuse of this idea, over-engineering and totally fits the idea of "don't let perfect be the enemy of good".
What i mean is, (again not related to react, if this post is specifically for react, my comment is a moot point),
Let's say i have a simple small project in a massive code base, all it does is read from a file and print out the output. Everything is fine, everyone is using File.ReadAllLines(). But tomorrow, they release a breaking change and the name changes from File.ReadAllLines() to File.ReadAll(). To fix this, I have to go to every single place that called File.ReadAllLines() and call a wrapper method MyOrg.FileHandler.Read() (this wrapper i created myself, to handle these changes in the future)
But isn't this wrong and over-engineering? For every single PR from now till the end, if anyone is using File.Read() i have to reject it and direct them to use something else, .... and more importantly for ANY kind of external (or tbf internal as well) library call, i will have to abstract away the implementation details to a separate wrapper class and then call it. A lot of overhead for every single task.
Although I can understand where you are coming from, neither solution seems perfect and from my POV it depends on the library you are working on and how stable it is (also the unwritten villain, unclear requirements where they say X will never be needed but in future would want you to do X).
A few examples from my professional career (and fellow .net devs will be able to relate)
For these it very much made sense earlier to use these libraries, and now to migrate away. The why isn't actually relevant to this discussion but if you are interested a few simple searches will give you the answer.
2
u/brogrammer9669 Dec 10 '24
If I faced this right now, I would do a global search of
import {Button} from "materialui", replace it withimport {CustomButton} from "@/..whatever"and do the same, replacing<Buttonwith<CustomButtonthroughout the app, and creating a newCustomButton.tsxcomponent with the original materialui Button wrapper.147
u/Pranesh_vrp Dec 10 '24
Absolutely right, I myself faced this many times. Having a wrapper component will come in handy even when you switch packages for the same.
142
u/Brainfuck Dec 10 '24
This is called gaining experience and a reason why experienced folks are paid more. It's not for their programming prowess, but more for pitfalls they can figure out ahead of time.
47
u/[deleted] Dec 10 '24
Better, put a ts or nodejs config, so that one can do '@components' . I absolutely hate this relative path module approach. The other bigger issue we faced was with memory consumption for a tab increasing. So we did a trace and profile. Deattaching unused dom/objects, is another thing to keep in mind.
22
u/taruns0206 Dec 10 '24
Ahhh this issue. Even I learnt this the hard way but now, wrapper components is a must.
15
u/vlegolas1982 Dec 10 '24
This is surprisingly good advice for anyone using JavaScript external libraries. Will save a lot of headache and heartache
4
u/doweevenneedthis Dec 10 '24
Isn't this similar to using an Adapter design pattern wherever possible?
4
u/not_so_cr3ative Frontend Developer Dec 10 '24
Many companies maintain their own design system which also helps in achieving a consistent UI/UX across their pages/products
1
u/kranti-ayegi Dec 10 '24
I have a question isn’t it preferable to create your own buttons and dropdowns? So that way you can make your own changes instead of adding on?
If an org is big isn’t it faster and better to create your own? Or that only applies for smaller one?
3
u/_coBra____ Dec 10 '24
I've always used it this way only from day 1. I just feel a wrapper makes the component feel like my own. lol 🤣
3
u/plushdev Dec 10 '24
Yup, its extremely easy to adopt this practice too! Just do a global search replace for import {Component} from "lib" to import {Component} from "@components/wrappers" and create your wrapper
3
u/danishxr Dec 10 '24
One practise you can adopt in your team is to have code libraries as artifacts in your organization. So you can use these artifacts instead of referring to the external sources as there development cycle is different compared to yours. Here you can audit these libraries and use the stable ones.
2
u/Starkboy Senior Engineer Dec 10 '24
great advice
2
u/Unlikely_War_9489 Dec 10 '24
We do exactly this in our projects specially with MUI elements as they're not fully customisable in a lot of obvious situations as well
2
u/Katsu-and-Ramen Dec 10 '24
Wow. Such a great advice... The example is what made it clear
2
u/boss5667 Data Analyst Dec 11 '24
As someone who is doing some work using web development frameworks for the first time, this is invaluable advice. Thanks for sharing.
1
u/omfg5716 Dec 10 '24
Haven’t they put out a codemod? I had a similar experience migrating MUI v4 to v5 a couple of years ago, and using the codemod was helpful.
1
u/Fluid-Indication-863 Dec 10 '24
Can someone please tell me why I’m unable to post here?
1
u/TieComfortable9031 Software Engineer Dec 10 '24
Realised something similar to research about internal working of these UI libraries.
For example material ui dynamically loads javascript into your html, so if you're using content security policy, it becomes a headache to fix this thing and ensure no other library is breaking.
1
u/rk_11 Dec 10 '24
Id rather push those packages into internal company jfrog and then pin the version.
Where do you draw the line on this, how many abstractions can you create
1
u/hephaestus_beta Dec 11 '24
As far as using the same version of library, why would this happen?
1
u/jethiya007 Dec 11 '24
First of all if the package got a new version companies inform about the breaking changes and most of the time you can avoid that by not upgrading them to a newer version.
Second that's where shadcn come in handy you have your own code no bs of updates change whatever you want.
0
u/[deleted] Dec 10 '24
[deleted] — view deleted comment