createComponent()
Creates a virtual DOM node for a component, allowing components to be used in the virtual DOM tree. This function is used to instantiate both class-based and functional components.Signature
Parameters
The component constructor (class) or functional component function.
Properties to pass to the component.
Child elements to pass to the component. These are automatically added to
props.children.Return Value
Returns a virtual DOM node object representing the component:The
instance property is set to the component instance when the component is mounted.Examples
Class Component
Functional Component
With Children
Usage in Views
When to Use createComponent()
UsecreateComponent() when you need to:
- Include a component in your virtual DOM tree
- Pass props to a component
- Use class-based or functional components in your views
- Compose complex UIs from reusable components
How It Works
- Virtual Node Creation:
createComponent()creates a special virtual node with typeCOMPONENT_TYPE - Mounting: When the virtual node is mounted:
- The component is instantiated with the provided props
- Functional components are wrapped in a
FunctionalComponentWrapper - The component’s
render()method is called - The resulting virtual DOM is mounted to the parent element
- Updates: When props change:
- The component’s
updateProps()method is called - The component re-renders with the new props
- The DOM is efficiently patched
- The component’s
Component Types
Class Components
Class components extend theComponent base class and must implement a render() method:
Functional Components
Functional components are pure functions that take props and return a virtual DOM node:Notes
- Children passed to
createComponent()are automatically added toprops.children - The component instance is not created until the virtual node is mounted
- Both class-based and functional components are supported
- Components can be nested arbitrarily deep
- Props are passed by reference, so avoid mutating them