DEV Community

Discussion on: What is the difference between Library vs Framework?

Collapse
 
peerreynders profile image
peerreynders • Edited

Some common examples of Library are: React

yet later:

In the Framework, you have to fill the structure accordingly with your code.

Creating a React component is filling that structure (even as a function component).

The main key difference between the Library and Framework is something known as inversion of control.

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.

Collapse
 
jackmellis profile image
Jack

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

Collapse
 
peerreynders profile image
peerreynders • Edited

For the same reasons I always feel like express is more library than framework.

1988 Johnson & Foote:

The methods supplied by the user tailor the generic algorithms defined in the framework for a particular application.

Looking at the express Hello World:

  • express() returns a generic express server.
  • app.get() "tailors" that generic server with user code - which will only be invoked by the express server.
  • app.listen() starts that generic server with the user code that "tailors" its operation ...

The framework often plays the role of the main program in coordinating and sequencing application activity

... 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:

  • If your code calls it, it's a library.
  • If it calls your code, it's a framework.

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".

Collapse
 
jonahgeek profile image
Jonathan Mwebaze

"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

Thread Thread
 
peerreynders profile image
peerreynders

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.

Collapse
 
snyntakk404 profile image
snyntakk404

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.

Some comments have been hidden by the post's author - find out more