Shouldn't FunctionComponent
be named ComponentFactory
?
#4827
Unanswered
ericmorand
asked this question in
Q&A
Replies: 1 comment 5 replies
-
Not quite. Under the hood it get's the same component instance as class components. It's not just a function that happens to return JSX. It's much more than that. The easiest way to demonstrate that is with hooks. Most of them rely on some form of storage being associated with the current component. That something needs to be stored somewhere for which we use the same component instance as for class components. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently teaching React to some students, from the bias of the library instead of the bias of the JSX language.
One of the thing that they had a hard time understanding was the distinction between a Component and a Component Type. Which I think is understandable.
For example, in this page:
https://preactjs.com/guide/v10/components#functional-components
Here,
MyComponent
is not a component: it is a Component Type - aka a factory that, when executed, returns a Virtual Node - the component itself.This is confirmed by the signature of
createElement
:Without any doubt,
createElement
expects its first parameter to be a Component Type (or a string).The example given above would look like this when using
createElement
:Which means that
MyComponent
is a Component Type.Now, this is where it gets really confusing: the
ComponentType
type is the union ofComponentClass
andFunctionComponent
.ComponentClass
conveys the fact that it is a factory that returns a component. But the typeFunctionComponent
does not: it actually means "a function that acts as a component", which does not make sense since a component is a Virtual Node, not a function.I was wondering where the naming rational was coming from and if I'm missing something. To make things understandable to my students, I had to explain to them that the naming
FunctionComponent
was a naming mistake, and that it actually meansComponentFactory
.But I'm myself still confused about why it was named
FunctionComponent
to begin with.Where is it coming from? What was the rational behind the naming choice? And would it be accepted if I propose a PR that adds
ComponentFactory
as an alias toFunctionComponent
, just likeFunctionalComponent
was added as an alias at some point?Beta Was this translation helpful? Give feedback.
All reactions