DEV Community

Cover image for React, Vue and Svelte: Comparing Lifecycle methods
Clément Creusat
Clément Creusat

Posted on • Updated on

React, Vue and Svelte: Comparing Lifecycle methods

Lifecycle in...

React

import { useEffect } from 'react';

const Component = () => {
  useEffect(() => {
    console.log("the component is now mounted.")
  },[])
}
Enter fullscreen mode Exit fullscreen mode
import { useEffect } from 'react';

const Component = () => {
  const [count, setCount] = useState(0);
  useEffect(() => {
    console.log("the component is updated.")
  },[count])
}
Enter fullscreen mode Exit fullscreen mode
import { useEffect } from 'react';

useEffect(() => {
    //your code goes here
    return () => {
      //your cleanup code codes here
      console.log("the component is destroyed.")
    };
},[]);
Enter fullscreen mode Exit fullscreen mode

Vue

Link

<script setup>
import { onMounted } from 'vue';

onMounted(() => {
  console.log("the component is now mounted.")
})
</script>
Enter fullscreen mode Exit fullscreen mode
<script setup>
import { onUpdated } from 'vue';

onUpdated(() => {
  console.log("the component is updated.")
})
</script>
Enter fullscreen mode Exit fullscreen mode
<script setup>
import { onUnmounted } from 'vue';

onUnmounted(() => {
  console.log("the component is destroyed.")
})
</script>
Enter fullscreen mode Exit fullscreen mode

Svelte

Link

<script>
  import { onMount } from 'svelte';
  onMount(() => {
    console.log("the component is now mounted.")
  });
</script>
Enter fullscreen mode Exit fullscreen mode
<script>
  import { afterUpdate } from 'svelte';
  afterUpdate(() => {
    console.log("the component is updated.")
  });
</script>
Enter fullscreen mode Exit fullscreen mode
<script>
  import { onDestroy } from 'svelte';
  onDestroy(() => {
    console.log("the component is destroyed.")
  });
</script>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)