While the terms Library and Framework may sound similar, they both work differently.
Many people use these two words interchangeably without know...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
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 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!
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.
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:
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"?
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".
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.
Muy bien explicado, gracias por la información
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!
nextjs is a framework too , thanks for clear explanation between framework and lib
Glad to hear that :)
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!!