The function passed to createMemo is called regardless of read #2204
Replies: 4 comments 8 replies
-
I just discovered that the docs recommend using That should be considered. I assumed a memo was like a function: only called when necessary. That page refers to it being like an effect: called up front and then at each change. |
Beta Was this translation helpful? Give feedback.
-
There is already a community primitive: https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createLazyMemo |
Beta Was this translation helpful? Give feedback.
-
@atk Neat! I wrote my own in desperation. Is this a documentation issue then? |
Beta Was this translation helpful? Give feedback.
-
BTW my workaround was: import { createMemo } from 'solid-js'
export function createCached(...params) {
let memo
return () => {
if (!memo) {
memo = createMemo(...params)
}
return memo()
}
} Is there anything wrong with this approach? Would a dynamically created memo be disposed correctly? |
Beta Was this translation helpful? Give feedback.
-
Hi,
I notice that the function passed to
createMemo
is called regardless of the value being read or not.Repro: https://playground.solidjs.com/anonymous/67b97e2b-ab4c-4cbc-b8ce-8ebf0f1e165c
I was surprised by this. It is not mentioned in the docs at https://docs.solidjs.com/reference/basic-reactivity/create-memo . I was trying to track down why my component suspended. Turns out I was making a computed value based on a resource, and even though I wrapped all read operations into Suspense, the component itself was still suspending because of createMemo calling the function outside the template.
I think if there is a reason for this behavior then it should be mentioned in the docs. If there is not, then I would be happy to submit a bug.
What is the case here?
Beta Was this translation helpful? Give feedback.
All reactions