Improve docs for handling the warning "computations created outside a createRoot
or render
will never be disposed"
#1379
Replies: 4 comments 3 replies
-
I found the solution, however, is not intuitive for the development experience the fact of manually specifying an owner inside an event listener. Why the owner is not auto-injected for event listeners in Solid?
|
Beta Was this translation helpful? Give feedback.
-
The dx can be improved(subjectively) with a utility/primitive import { getOwner, runWithOwner } from "solid-js";
//stay liquid
/**
* provide a default owner to a callback.
* if there is an active owner, the active owner takes precedence.
*/
export const withOwner =
(fn, o = getOwner()) =>
(...args) => {
o ??= getOwner();
return o ? runWithOwner(o, () => fn(...args)) : fn(...args);
};
export const useEvent = withOwner usage:const myEventHandler = useEvent(()=>{
createEffect(()=>{})
})
<>
<button onClick={myEventHandler}></button>
</>
or directly in JSX element
<>
<button onClick={useEvent(()=>{ createEffect(()=>{} ) })}></button>
</> |
Beta Was this translation helpful? Give feedback.
-
The thinking is creation under async is sort of dangerous. The warning is precisely to discourage this. Because yes without the hook rules you can do exactly this and it all works and doesn't lose reactivity, but it also isn't a great thing to do. Top level computations are fine as they are probably intended as global, but ones created during timeouts, async callbacks, and event handlers can be called repeatedly and get lost. And I have no intention of trying to patch every possible async callback. The owner in this case is what? The scope the event handler is declared in? That means you click 100 times you create 100 effects that only get disposed of when that scope cleans up. The current setup sort of pushes entry into the system through data. If you want to create an effect, set a signal that maps it. It would be interesting to see a logical way to remove this constraint that wouldn't lead people into temporary memory leaks. But mostly I'm curious of the scenario you want this. Like what are you attempting to accomplish? I think talking through that would be more interesting to understand. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm seeing a potential Achilles heel in Solid because is very easy to fall on this warning, and in some cases, the reactivity is lost.
computations created outside a `createRoot` or `render` will never be disposed
Despite the rules of hooks doesn't apply to SolidJS, invoking a hook (that contains effects) inside a function triggered by an onClick event shows this warning. Here is a demo example at SolidJS playground:
https://playground.solidjs.com/anonymous/57dfbe9b-80e3-4a6a-a445-33b7c6dcf87e
Solutions for mitigating this problem:
For my part, I don't know how to give a solution for the given playground example.
Beta Was this translation helpful? Give feedback.
All reactions