While the terms Library and Framework may sound similar, they both work differently.
Many people use these two words interchangeably without knowing the profound meaning behind them.
Before we dig into the key differences between Library and Framework, let's look at the common purpose that they both serve.
Both Library and Framework are code written by some developer to solve a complicated problem efficiently.
They both give you an excellent approach to write DRY (don't repeat yourself) code.
Their purpose was to increase the reusability of the code so that you can use the same piece of code or functions again in your various project.
What is Library?
A Library is a set of code that was previously written by a developer that you can call when you are building your project.
In Library, you import or call specific methods that you need for your project.
In simple words, a bunch of code packed together that can be used repeatedly is known as Library.
Reusability is one of the main reasons to use libraries.
Let's understand this more clearly with the help of an example.
Think of you as a carpenter who needs to build a table.
Now, you can build a table without the help of tools, but it's time-consuming and a long process.
Whereas, if you choose the correct tools, you'll be able to build a table more quickly and that too without any hardship.
Think of here tools as a library. You can write your program without them.
But it will be a long process, and chances are your program will get buggy, while if you use Library, it'll be much easier for you to work with the program.
For example, if you use the in-built JavaScript fetch () method to fetch the data from API and you feel that it's not the ideal solution.
Then you can use Axios Library for the same purpose to make your work easier.
axios.post('/login', {
firstName: 'Monica',
lastName: 'robinson '
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
Some common examples of Library are:
React is a JavaScript library for building user interfaces.
Redux
Redux is an open-source JavaScript library for managing application state.
It's most commonly used with React
Three.js
It's another super cool JavaScript library used to create and display 3d computer graphics.
Lodash
Lodash is a JavaScript library that provides utility functions for common programming tasks.
It's more of a productivity kit in node.js
jQuery
jQuery is a JavaScript library that does the things like event handling and HTML document manipulation.
What is Framework?
A framework is a supporting structure that gives shape to your code.
In the Framework, you have to fill the structure accordingly with your code.
There is a specific structure for a particular framework that you have to follow, and it's generally more restrictive than Library.
One thing to remember here is that frameworks sometimes get quite large, so they may also use the Library.
But the Framework doesn't necessarily have to use Library.
Let's get back to our carpenter and table example for a better understanding of the Framework.
Here, if you want to build a table, then you need a model or skeleton for how the table looks, like the table has four legs and a top slab.
Now, this is the core structure of the table and you have to work accordingly to build the table.
Similar to this, Framework also provides the structure, and you have to write the code accordingly.
Let's take the example of Express and understand the restrictive nature of the Framework.
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('welcome to dev.to!! ')
})
app.post('/', function (req, res) {
res.send('POST request to the dev.to homepage')
})
Here express is designed in such a way that it is going to look only for specific methods (get/post) and specific parameters.
You can't name the methods whatever you want to, and you have to name the methods as per the documentation.
Some common examples of Framework are:
Angular is a JavaScript framework for web and mobile development.
Django
Django is a fully featured server-side web framework written in
Python.
Express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
Rails
Rails is a web application development framework written in the Ruby programming language.
Spring
Spring Framework is an open-source framework for building web applications with Java as a programming language.
Key Difference between Library vs Framework
The main key difference between the Library and Framework is something known as inversion of control.
Let's understand this inversion of control more in detail.
When you import a library, you have to call the specific methods or functions of your choice so, and it's up to you when and where to call the Library.
Here, you are in charge of flow.
On the other hand, Framework itself makes a call to your code and provide you with some space to write down details.
So, while using framework your framework is in charge of flow.
In Library, your code is going to call the Library whereas, in Framework, your code is being called by Framework.
Conclusion
I know it's getting too confusing but stay with me. I'll end this with one last crucial point.
Here is a simple thing to remember Framework is often more restrictive and generally have a more set of rules.
Whereas, Library is not bounded by many rules.
I hope you get a broader perspective of what's the difference between Library and Framework.
If you find my work interesting and worth reading you can appreciate me on Twitter and LinkedIn.
All kinds of Feedback are welcomed in comments.
Top comments (31)
yet later:
Creating a React component is filling that structure (even as a function component).
Correct.
Your React components are managed/invoked by React. By calling
ReactDOM.render()
you hand over the UI/main thread to React. Past that point the components may use the tooling provided by React - but the components only get control when React decides to give them control.For more details React is a (view component) framework.
One could argue that by calling itself a library React has greatly contributed to the library vs framework confusion.
This. I've never understood the insistence that react is not a framework as it totally is. Maybe it was originally a library, but the fact that we now build "react apps" is an obvious sign that it's a framework.
To me a framework is something that drives and control a major part of your code, whereas a library just helps you in writing your code.
For the same reasons I always feel like express is more library than framework. Essentially its just a thin wrapper around the http module, and aside from setting up routes, it has no control or interest in how the rest of your app works
1988 Johnson & Foote:
Looking at the express Hello World:
... which is exactly what
app.listen()
accomplishes. So express ticks all the boxes for a framework - while the main thread configures the generic server, once running, the generic server calls all the user code that was supplied via configuration.It really comes down to:
Off course by that definition the http module itself is a "framework" ...
node.js Hello world
... because the module "plays the role of the main program" and the module "calls the user code".
This just illustrates that a "framework" doesn't have to be big, complex or "all batteries included".
"Something that drives and controls a major part of your code", don't you think this makes create-react-app the framework and reactjs just a library, just like react-scripts, react-dom, redux etc... We don't build react apps, we use react to build applications. React is just part of it, just like react-dom or even react-router-dom
TL;DR:
"Framework" !== "Application Framework"
See my other comment.
React is the core that is responsible for "the operation in the manner of a framework" - orchestrating the application activities and only delegating to user code via inversion of control.
React components (user code) aren't being run by the event loop but by React.
So while React may look like it's just a layer between the components and the JavaScript runtime it's actually React running (calling) the components - components don't use React unless React calls them first.
React is a library still, because the idea with it (and any other library that uses VDOM or some other reactive engine), is that it takes care of the DOM mutations for you, so you can design your components and focus in the actual logic instead of dealing with the DOM yourself.
You define how to handle routing, testing, styling, state, component style and so on.
From your point of view, React would have to give you control over the VDOM trough functions that you'll call to "be a library", which is kinda ludicrous. At that point you should just implement the VDOM yourself.
On the other hand you have actual frameworks like Angular, in which you have to use classes, with their decorators, their template system, their cli tools, their testing engine, their services, their routing, and so on (and yet the way the components work in there still sucks 😅).
In short: React is an abstraction layer of the DOM, that includes some optional useful tools to handle state (that you can use if you want). But that's it. Everything else is up to you. You can even opt out of JSX if you want to. Angular takes all those decisions and more for you.
I fail to see how that is in any way ludricrous. Call it a VDOM framework then.
What exactly does the JSX
<p>{count}</p>
do? It desugars to:So a React (functional) component's return value is a ReactElement.
2005 Martin Fowler: InversionOfControl
So while functional components have eliminated the need to subclass
React.Component
, functional components are "plugged into" React because that is how React creates the component tree and each and every functional component returns aReactElement
- a type that is specific to React and created by React viaReact.createElement
. And the relevant location for thatReactElement
is determined by the location of the component instance within the component tree.A simple working example:
That last
render
statement is where the "inversion of control" kicks in.render
is the framework function that "plays the role of the main program".Contrast that to µhtml which is a library:
html
is a tagged template which returns an object that is designed to produce something that extends HTMLElement a platform type (not a framework type).render
is a convenience function that attaches that DOM element instance to the indicated platform location.In terms of the script the final statement invokes a user function (not a framework function) - so the user code is always running the show - so there is no framework in control here.
Interestingly µhtml is the foundation for µland - a React-style framework:
Component
)render
, a framework function, exercising "inversion of control".Why is it that people associate "framework" with size and complexity? The criteria for categorizing software as a "framework" were established long before either Angular or React were created. While the statement "React isn't an application framework like Angular" may be correct that in no way implies that React isn't a framework in its own right.
Why is it that people act as if the sky is going to fall on their head if they were to utter "React is a framework"?
You mentioned it in your example, when you write:
Turns into:
So basically, every time you write JSX, you're calling a function, just with a more expressive syntax. You can then call yet another function to render that into the place of the DOM you desire:
If you don't call that function, or you desire to transform the output of the
Anchor
before doing so, or you prefer to render it as plain text instead of using the DOM, or whatever else, then you just do so. As I mentioned before, React is just abstracting the complexity of the DOM, and that's it.Contrast that with doing it by hand:
The main difference is that with that "vanilla approach" I now need to take care of every update in the DOM by hand, while React's render does that for me. Yes, it is calling my functions by itself, but is because it basically took a really outdated API (the DOM) and wrapped it with the VDOM enabling a functional approach to the DOM (one-way data flow), instead of those disastrous double bindings of the past.
I used both
µhtml
andµland
in the past (I generally try out everything made by WebReflection because he is a fantastic developer), but your example withµhtml
you're just callingrender
in a fixed interval, but thatrender
expects the output ofhtml
, which yes is aHTMLElement
, but how is that different from using a plain object with a convention (besides the fact that HTMLElement is way more complex than needed)? React is just making that "interval" for you. Does that turn it into a framework from your PoV? Would you take React as a library if it let you callrender
by intervals controlled by you?PS: If you like the tagged template approach, there is a great library by developit (the creator of preact) that gives you that, but you define what to call when the function is used: github.com/developit/htm
And he never tires of pointing out to everybody that React is a costly abstraction (1, 2, 3, 4, 5, 6, 7, 8).
Which is exactly what one does when rendering with a library.
The point is that a functional component is nothing but a glorified (but "tailored")
render()
function.But the difference is that in the case of µhtml the user code is always in control -
html
andrender
are simply helper functions for generating and placing the DOM nodes so that the user code doesn't have to deal with them directly making the user code more declarative than manipulating the DOM manually.In React the user code doesn't call function components ("tailored render functions"); the function components are user code which are handed over to React which then invokes them as it deems fit - that is what makes React a framework.
That said I have yet to run across a VDOM library - all VDOM abstractions I have encountered are operating in the manner of a framework.
I'm familiar with Jason Miller's work.
I don't get how
html
andrender
from µhtml are "just helper functions" but_jsx
andrender
from React aren't that as well from your PoV. I mean your µhtml components are also handed over to µhtml'srender
. The way I see it:_jsx
takes some parameters and returns an object.html
takes a template string and returns anHTMLElement
.render
then take some parameters and deal with the complexity of the DOM for you.What's the concrete thing that from your Point of view makes React a framework and µhtml a library? I mean I would get it if you considered both a library or both a framework, but you actually see them as two different things.
PS: I noticed that you edited your comment and added links to Andrea's tweets and examples, but in one of those tweets you can see that he says:
They don't stop being libs just because they have other patterns.
Also, just in case, I'm not advocating in favor of React because of performance/size, because there are libraries out there way better than React (like Preact). I'm just saying that it makes sense to call it a library.
update()
function ?update(START)
- i.e. user code. By extensionhtml
andrender
are only called by user code.App()
component? React.render(h(App), document.querySelector('.js-root'));
only hands over the component to React - it's up to React to invoke it. By extension any code inside ofApp()
(the "user code") only runs when React wants it to.As I wrote in my second comment:
Isn't µhtml calling my component as well? Isn't the developer the one calling React's
render
?Both
render
functions need to be called by the user and take something from the user and either use it, or call it when they need to do it. The difference is that React's render doesn't make you do the update, while µhtml does.So it something like:
If that's enough to consider React a framework, then ok, there are lots of libraries out there that should be called frameworks instead.
It might be worth mentioning that Frameworks and libraries have different definitions in FE vs the classic. I mean in the FE generally we consider something a Framework when it comes bundled with an answer to every question (routing, testing, components, and so on) and is extremely opinionated, while we consider a library something that doesn't do that (unopinionated and good for one thing). That's why classically we put React in the library bucket and Angular in the Framework bucket.
Still, I guess we can close it in a "we agree to disagree". I mean I think I get why you consider React a Framework, but lets say that if it actually is a framework, is an extremely simple and unopinionated one, that only takes care of your interactions with the DOM.
The point is by the time
render()
returns nothing of µhtml is left of the call stack. The user code is in complete control.The relevant difference is that with React your code calls
render()
once during the entire lifetime of the application session and React remains active for the remainder of that session - orchestrating all actions that the application takes from that point on (i.e. React is in charge).µhtml's
render()
has to be called whenever your code wants to patch the DOM; sorender()
is called many times during the application's lifetime - and whenrender()
returns your code is completely in the driver seat - not µhtml.What you are describing is an application framework. Not all frameworks are application frameworks. So if you are correct that means that in FE development application framework has been contracted to framework narrowing the meaning of the term significantly.
At this point your perspective seems to be:
My perspective is:
I think at this point it's clear which view I find more obvious and less confusing.
Calling React unopinionated is in my view misleading at best. It certainly is less opinionated than Angular but that isn't the same thing as being unopinionated. In fact choosing React as your abstraction has a significant impact on your client side solution architecture.
Redux maintainer Mark Erikson made an astute observation:
The majority of Kent C. Dodds's article Application State Management with React explores the component-centric approach which goes beyond "taking care of interactions with the DOM" - it essentially uses React as if it was an application framework to the point that "React is your Application".
The "app-centric" approach tries to separate the application from React, narrowing React's responsibilities to just "taking care of interactions with the DOM". However as soon as you use an integration like React-Redux you are coupling your React code with the way Redux operates, trading away the opportunity to cleanly separate your UI from the client-side aspect of your application (and as David Khourshid points out Redux has its own set of trade offs).
In UI As An Afterthought Michel Weststrate discusses how he builds the client-side application around MobX rather than having the UI abstraction dictate the structure of the application (a modern take on Segregated DOM). Having a clear boundary between React (the UI) and the rest of the client-side application is important to use web workers effectively.
The point is that you have to be pretty disciplined and determined (i.e. it's not the path of least resistance) if you want React to just "take care of interactions with the DOM".
The way I see it, if you let React become the center of the universe (meaning your app/project), you got yourself a framework. I believe strongly opinionated frameworks are not the only kind of framework.
React is more of a mix of both framework and library. It has the key pieces of a library but contains concepts of a framework. Thus I classify it as both with it leaning towards framework. This was a great article but it some editing with the examples.
Great Post! Thanks for sharing!
Muy bien explicado, gracias por la información
nextjs is a framework too , thanks for clear explanation between framework and lib
Glad to hear that :)
thanks for the update pal, and I agree that react is more of a library than of a framework.
That is a great explanation of 2 key concepts for beginners!
Thanks!
Thanks! This is helpful.
I'm glad it helped you
Good post, thanks ! Also upvoted! 🥳
Thanks ! for the appreciation
You invoke/use the code from library, whereas a framework invokes/uses your code.
Exactly!!
Some comments have been hidden by the post's author - find out more