The function passed to createMemo is called regardless of read #2204
Closed
andraaspar
started this conversation in
General
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. |
0 replies
|
There is already a community primitive: https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createLazyMemo |
1 reply
|
@atk Neat! I wrote my own in desperation. Is this a documentation issue then? |
1 reply
|
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? |
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I notice that the function passed to
createMemois 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?
All reactions