Last active
October 10, 2024 05:52
-
-
Save kirilkirkov/1eb8593a7c9aa5bd93ce1459eed5493d to your computer and use it in GitHub Desktop.
Example of how Vue and React works when change their states - in react all javascript is executed, in Vue only the template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
function App() { | |
const [count, setCount] = useState(0); | |
// execute each time when click the button | |
console.log(Math.random()) | |
useEffect(() => { | |
// execute each time when click the button | |
console.log(Math.random()) | |
}); | |
return ( | |
<!-- RERENDER ALL DOM --> | |
<div> | |
<button type="button" onClick={() => setCount(count + 1)}> | |
Click {count} | |
</button> | |
</div> | |
); | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script setup> | |
import { ref, onMounted, watch, computed } from 'vue' | |
const a = ref(1) | |
// executed one time only | |
console.log(Math.random()) | |
onMounted(() => { | |
// executed one time only | |
console.log('test') | |
}) | |
const getA = computed(() => { | |
return '-->' + a.value | |
}) | |
const increase = () => { | |
a.value = a.value + 1 | |
} | |
watch(a, () => { | |
console.log('a increased') | |
}) | |
</script> | |
<template> | |
<!-- RERENDER ALL DOM --> | |
<div> | |
{{ getA }} | |
<button type="button" @click="increase()">click</button> | |
</div> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any changes in Vue triggers the exact amount of re-render. In React, any state change will trigger that entire component sub-tree to re-render.
React rerender full function, but Vue only the template.
Vue
Any imported (child) component into the vue-rerender.vue with same Math.random() functions, will execute only one time regardless each increment!
React
Any imported (child) component into the react-rerender.js with same Math.random() functions, will execute each time on each increment!