1. Summary
Implement a factory function that allows developers to define functional components using a plain JavaScript function, abstracting away the underlying class creation.
2. Rationale
This factory further simplifies the developer experience for "beginner mode" functional components, making the syntax more concise and familiar to developers accustomed to functional component patterns in other frameworks. It acts as the bridge between a pure function definition and the underlying Neo.functional.component.Base class.
3. Scope & Implementation Plan
- Create File: Create
src/functional/defineComponent.mjs.
- Implement
defineComponent: The factory will accept a configuration object (including className, optional ntype, and the createVdom function).
- Internal Class Generation: Internally,
defineComponent will create a new class that extends Neo.functional.component.Base. It will apply the provided className and ntype to this new class.
createVdom Method Assignment: The developer's createVdom function will be assigned as the createVdom method of the generated class's prototype.
- Integration with
useConfig: Ensure that the context (this) within the developer's createVdom function (when executed by the generated component instance) allows useConfig to correctly associate state with that instance.
4. Example Usage
import { defineComponent } from 'neo/functional/defineComponent.mjs';
import { useConfig } from 'neo/functional/useConfig.mjs';
const MyGreeting = defineComponent({
className: 'MyApp.MyGreeting',
createVdom: (config) => {
const [name, setName] = useConfig('World');
return {
tag: 'div',
text: `Hello, ${name}!`,
listeners: {
click: () => setName(name === 'World' ? 'Neo.mjs' : 'World')
}
};
}
});
5. Definition of Done
Neo.functional.defineComponent factory is implemented and tested.
- It successfully generates functional component classes from plain functions.
- Generated components correctly utilize
useConfig for state management.
1. Summary
Implement a factory function that allows developers to define functional components using a plain JavaScript function, abstracting away the underlying class creation.
2. Rationale
This factory further simplifies the developer experience for "beginner mode" functional components, making the syntax more concise and familiar to developers accustomed to functional component patterns in other frameworks. It acts as the bridge between a pure function definition and the underlying
Neo.functional.component.Baseclass.3. Scope & Implementation Plan
src/functional/defineComponent.mjs.defineComponent: The factory will accept a configuration object (includingclassName, optionalntype, and thecreateVdomfunction).defineComponentwill create a new class that extendsNeo.functional.component.Base. It will apply the providedclassNameandntypeto this new class.createVdomMethod Assignment: The developer'screateVdomfunction will be assigned as thecreateVdommethod of the generated class's prototype.useConfig: Ensure that the context (this) within the developer'screateVdomfunction (when executed by the generated component instance) allowsuseConfigto correctly associate state with that instance.4. Example Usage
import { defineComponent } from 'neo/functional/defineComponent.mjs'; import { useConfig } from 'neo/functional/useConfig.mjs'; const MyGreeting = defineComponent({ className: 'MyApp.MyGreeting', createVdom: (config) => { const [name, setName] = useConfig('World'); return { tag: 'div', text: `Hello, ${name}!`, listeners: { click: () => setName(name === 'World' ? 'Neo.mjs' : 'World') } }; } });5. Definition of Done
Neo.functional.defineComponentfactory is implemented and tested.useConfigfor state management.