docs/docs/en/runjs/context/off.md
Removes event listeners registered via ctx.on(eventName, handler). It is often used in conjunction with ctx.on to unsubscribe at the appropriate time, preventing memory leaks or duplicate triggers.
| Scenario | Description |
|---|---|
| React useEffect Cleanup | Called within the useEffect cleanup function to remove listeners when the component unmounts. |
| JSField / JSEditableField | Unsubscribe from js-field:value-change during two-way data binding for fields. |
| Resource Related | Unsubscribe from listeners like refresh or saved registered via ctx.resource.on. |
off(eventName: string, handler: (event?: any) => void): void;
React.useEffect(() => {
const handler = (ev) => setValue(ev?.detail ?? '');
ctx.on('js-field:value-change', handler);
return () => ctx.off('js-field:value-change', handler);
}, []);
const handler = () => { /* ... */ };
ctx.resource?.on('refresh', handler);
// At the appropriate time
ctx.resource?.off('refresh', handler);
handler passed to ctx.off must be the same reference as the one used in ctx.on; otherwise, it cannot be correctly removed.ctx.off before the component unmounts or the context is destroyed to avoid memory leaks.on/off methods