SolidJSS
SolidJS8mo ago
24 replies
pao ramen

onCleanup and props

I thought I knew how onCleanup works.. but I don't.

It would be great to understand the component lifecycle since the documentation barely explains how createEffect/onMount/onCleanup really work.
Here is the piece of code that I find strange:

function Component(props: { post: Post}) {
  onCleanup(() => console.log(props.post.id)) // this breaks, since post is null on cleanup? 
  return <span>{post.title}</span>
}

function Component(props: { post: Post}) {
  const post = createMemo(() => props.post)
  onCleanup(() => console.log(post().id)) // but this works
  return <span>{post.title}</span>
}


It would be great to understand why props are null during cleanup, and why memos are not. And perhaps document it, since seems rather obscure.
Was this page helpful?