It's very similar to inheritance where some properties are passed from parent to child.
Inheritance is a is-a relationship. The relationship between a container and nested component is a has-a relationship.
Early React even described it as the owner-owneerelationship but community usage moved towards "parent-child" even though that makes absolutely no sense in reference to props.children.
Note how the community usage makes matters a lot more confusing.
Avatar is the Parent to div, ProfilePic, ProfileLink
ProfilePic, ProfileLink are passed via props.children to div
Two entirely different sets of parent-child relationships for everyone. And worse div has very little "parental control" over props.children short of ignoring them which is largely what happens in user components even though props.children is React's equivalent to WC's unnamed slot.
You use
<Childrenchildcard={card("Child")}/>
What's wrong with using props.children instead:
<Children><Cardtitle="Child"/><Children/>
which preserves the familiarity with markup better?
you can actually send HTML to your child like any card body
There is no actual HTML or even DOM involved; that is all react-dom's job'. JSX is a DSL for representing the React component tree where each component instance is responsible for rendering ReactNodes.
"JSX is an XML-like syntax extension to ECMAScript without any defined semantics." JSX specification
JSX doesn't follow HTML formatting rules but stricter XML rules and is a JavaScript extension that in some framework specific way compiles to some JavaScript code that will eventually lead to some DOM elements being manipulated/created.
In the case of React the component tree is assembled; in the case of SolidJS actual DOM nodes are created to be included in the page's DOM - no HTML.
JSX's representation evokes the familiarity of markupβbut it isn't markup.
Hi π
This is Suchintan a Full Stack Developer with MERN stack.
Want to know more about me?
Connect me on - https://www.linkedin.com/in/suchintan-das-b698bb1b8/
Thanks peerreynders, for writing this much. It's really a huge chunk of information that you shared and I really appreciate your hard work and efforts.
I'm about to comment on that HTML part. JSX is just a representation of virtual DOM using HTML like syntax, however all the JavaScript code is generated by tools like Babel.
You can play around online Babel compiler and see for yourself how your JSX turns into js code which is what is handled by react.
Hi π
This is Suchintan a Full Stack Developer with MERN stack.
Want to know more about me?
Connect me on - https://www.linkedin.com/in/suchintan-das-b698bb1b8/
Thanks naveennamani, for providing the link and the information.
Yes, I was also familiar with the fact that JSX is actually an extension provided by React to help us write the same code in HTML and in the backdoor it changes all the stuffs to JavaScript . Making the whole process less tiring for the developers .
But I avoided to dig that deep into the inner functionality while explaining the stuff so that any beginner can also easily grab the tip without striking their head over the whole backdoor mechanism.
Forget about the other details with regards to JSX. However referring to JSX as HTML is problematic especially in "teaching mode" as it perpetuates the wrong mental model.
JSX lets you declaratively compose React components. That's all there is to it.
Hi π
This is Suchintan a Full Stack Developer with MERN stack.
Want to know more about me?
Connect me on - https://www.linkedin.com/in/suchintan-das-b698bb1b8/
Peerreynders, I think you haven't read the whole sentence that I had written. I would request you to please read the whole thing as communication gap does create a lot of confusions.
In the whole line I said "JSX is actually an extension provided by React to help us write the same code in HTML and in the backdoor it changes all the stuffs to JavaScript" . I am not referring JSX as HTML, I pointed out the fact here that JSX is an extension that is provided by React to help developers write the same format of code as HTML into JavaScript with minimal changes.
I may understand what you mean but I'm convinced that your target audience is not going to come away with the same understanding.
write the same format of code as HTML
That phrasing alone is enough to establish a false equivalence between JSX and HTML. And to some degree it's only valid as long as you are talking about React's DOM element components. User components compose those DOM components. Container components may only compose User components without adding any additional DOM components. Providers have no equivalence in the DOM at all.
JSX is what makes React declarative. HTML happens to be declarative. JSX is XML. Both XML and HTML derived from SGML.
JSX composes React Components
HTML composes markup from HTML elements
The fact that a component's position in the component tree is linked to the position of the rendered content in the DOM tree is a leaky abstraction.
React's entire premise is to reuse React components in React Native. React Native is all components and JSX; no HTML, no DOM;
Hi π
This is Suchintan a Full Stack Developer with MERN stack.
Want to know more about me?
Connect me on - https://www.linkedin.com/in/suchintan-das-b698bb1b8/
Thanks peerreynders for the feedback. Yes, there a case that people take away a wrong concept by misunderstanding this line. I will edit this line on the blog so that atleast no misconception is spread.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Inheritance is a is-a relationship. The relationship between a container and nested component is a has-a relationship.
Early React even described it as the owner-ownee relationship but community usage moved towards "parent-child" even though that makes absolutely no sense in reference to
props.children
.Old usage:
Avatar
div
,ProfilePic
,ProfileLink
div
is the parent toProfilePic
,ProfileLink
ProfilePic
,ProfileLink
are children todiv
Community Usage:
Avatar
div
,ProfilePic
,ProfileLink
div
is the parent toProfilePic
,ProfileLink
ProfilePic
,ProfileLink
are children todiv
Note how the community usage makes matters a lot more confusing.
Avatar
is the Parent todiv
,ProfilePic
,ProfileLink
ProfilePic
,ProfileLink
are passed viaprops.children
todiv
Two entirely different sets of parent-child relationships for everyone. And worse
div
has very little "parental control" overprops.children
short of ignoring them which is largely what happens in user components even thoughprops.children
is React's equivalent to WC's unnamed slot.You use
What's wrong with using
props.children
instead:which preserves the familiarity with markup better?
There is no actual HTML or even DOM involved; that is all
react-dom
's job'. JSX is a DSL for representing the React component tree where each component instance is responsible for rendering ReactNodes."JSX is an XML-like syntax extension to ECMAScript without any defined semantics."
JSX specification
JSX doesn't follow HTML formatting rules but stricter XML rules and is a JavaScript extension that in some framework specific way compiles to some JavaScript code that will eventually lead to some DOM elements being manipulated/created.
In the case of React the component tree is assembled; in the case of SolidJS actual DOM nodes are created to be included in the page's DOM - no HTML.
JSX's representation evokes the familiarity of markupβbut it isn't markup.
Thanks peerreynders, for writing this much. It's really a huge chunk of information that you shared and I really appreciate your hard work and efforts.
I'm about to comment on that HTML part. JSX is just a representation of virtual DOM using HTML like syntax, however all the JavaScript code is generated by tools like Babel.
You can play around online Babel compiler and see for yourself how your JSX turns into js code which is what is handled by react.
https://babeljs.io/repl/#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=DYUwLgBAHhC8A8ATAlgNwHwAsTGAewgHc8AnYReAehQyA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2&prettier=false&targets=&version=7.18.5&externalPlugins=&assumptions=%7B%7D
Thanks naveennamani, for providing the link and the information.
Yes, I was also familiar with the fact that JSX is actually an extension provided by React to help us write the same code in HTML and in the backdoor it changes all the stuffs to JavaScript . Making the whole process less tiring for the developers .
But I avoided to dig that deep into the inner functionality while explaining the stuff so that any beginner can also easily grab the tip without striking their head over the whole backdoor mechanism.
As another commenter already pointed out to you:
Forget about the other details with regards to JSX. However referring to JSX as HTML is problematic especially in "teaching mode" as it perpetuates the wrong mental model.
JSX lets you declaratively compose React components. That's all there is to it.
Peerreynders, I think you haven't read the whole sentence that I had written. I would request you to please read the whole thing as communication gap does create a lot of confusions.
In the whole line I said "JSX is actually an extension provided by React to help us write the same code in HTML and in the backdoor it changes all the stuffs to JavaScript" . I am not referring JSX as HTML, I pointed out the fact here that JSX is an extension that is provided by React to help developers write the same format of code as HTML into JavaScript with minimal changes.
I may understand what you mean but I'm convinced that your target audience is not going to come away with the same understanding.
That phrasing alone is enough to establish a false equivalence between JSX and HTML. And to some degree it's only valid as long as you are talking about React's DOM element components. User components compose those DOM components. Container components may only compose User components without adding any additional DOM components. Providers have no equivalence in the DOM at all.
JSX is what makes React declarative. HTML happens to be declarative. JSX is XML. Both XML and HTML derived from SGML.
The fact that a component's position in the component tree is linked to the position of the rendered content in the DOM tree is a leaky abstraction.
React's entire premise is to reuse React components in React Native. React Native is all components and JSX; no HTML, no DOM;
Thanks peerreynders for the feedback. Yes, there a case that people take away a wrong concept by misunderstanding this line. I will edit this line on the blog so that atleast no misconception is spread.