Lifecycle Solid — SolidJS

Solid menyediakan lifecycle functions yang sederhana. onMount import { onMount } from "solid-js" function App() { let inputRef onMount(() => { // Dipanggil seka

Solid menyediakan lifecycle functions yang sederhana.

onMount

import { onMount } from "solid-js"

function App() {
  let inputRef

  onMount(() => {
    // Dipanggil sekali setelah render pertama
    inputRef.focus()
  })

  return <input ref={inputRef} />
}

onCleanup

import { onCleanup } from "solid-js"

function Timer() {
  const [count, setCount] = createSignal(0)
  const timer = setInterval(() => setCount(c => c + 1), 1000)

  onCleanup(() => clearInterval(timer))

  return <p>{count()}</p>
}

onMount hanya berjalan sekali. onCleanup dipanggil saat komponen dihapus atau owner scope di-dispose.

Yang akan kamu pelajari