Skip to content

Commit 9ee89e8

Browse files
committed
guides/ExtendingNeoClasses: Overriding Lifecycle Hooks #6918
1 parent 1166d24 commit 9ee89e8

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

learn/guides/ExtendingNeoClasses.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,68 @@ For every reactive config (`myConfig_`), Neo.mjs provides three optional lifecyc
6161
* **Purpose**: Intercepts the value *before* it is returned by the getter. Useful for lazy initialization, computing values on demand, or returning a transformed version of the stored value.
6262
* **Return Value**: Return the `value` that should be returned by the getter.
6363

64+
### Overriding Lifecycle Hooks: `super` vs. Full Override
65+
66+
When extending a Neo.mjs class, you often need to customize the behavior of inherited lifecycle hooks (like `afterSet*`, `onConstructed`, etc.). You have two primary approaches:
67+
68+
#### 1. Extending Parent Behavior (Calling `super`)
69+
70+
This is the most common and recommended approach. By calling `super.methodName(...)`, you ensure that the parent class's implementation of the hook is executed. You can then add your custom logic either before or after the `super` call.
71+
72+
This approach is crucial for maintaining the framework's intended behavior and ensuring that inherited features continue to function correctly.
73+
74+
```javascript readonly
75+
import Button from '../../src/button/Base.mjs';
76+
77+
class MyExtendedButton extends Button {
78+
static config = {
79+
className: 'My.Extended.Component',
80+
// text_ config is inherited from Button.Base
81+
// We can set a default value here if needed, or rely on button.Base's default
82+
text: 'New Default Text'
83+
}
84+
85+
// Example: Adding logic after the parent's afterSetText
86+
afterSetText(value, oldValue) {
87+
// Add your custom pre-processing logic here
88+
super.afterSetText(value, oldValue);
89+
console.log(`Custom logic: Button text changed to "${value}"`);
90+
// Add your custom post-processing logic here
91+
}
92+
}
93+
94+
export default Neo.setupClass(MyExtendedButton);
95+
```
96+
97+
#### 2. Completely Overriding Parent Behavior (No `super` Call)
98+
99+
In rare cases, you might want to completely replace the parent class's implementation of a hook. This is achieved by simply omitting the `super` call within your overridden method.
100+
101+
**Caution**: Use this approach with extreme care. You must fully understand the parent's implementation and ensure that your override does not break essential framework functionality or inherited features. This is generally reserved for advanced scenarios where you need full control over the hook's execution.
102+
103+
```javascript readonly
104+
import Button from '../../src/button/Base.mjs';
105+
106+
class MyFullyOverriddenButton extends Button {
107+
static config = {
108+
className: 'My.Fully.Overridden.Component',
109+
text : 'New Default Text'
110+
}
111+
112+
// Example: Completely overriding afterSetText
113+
afterSetText(value, oldValue) {
114+
// No super.afterSetText(value, oldValue); call
115+
console.log(`Fully custom logic: Button text changed to "${value}"`);
116+
// The parent's afterSetText will NOT be executed
117+
// This means that in this case you need to take care on your own to map the text value to the vdom.
118+
}
119+
}
120+
121+
export default Neo.setupClass(MyFullyOverriddenButton);
122+
```
123+
124+
125+
64126
```javascript readonly
65127
class MyHookedClass extends Neo.core.Base {
66128
static config = {

0 commit comments

Comments
 (0)