useState
and useEffect
from React, a local state is added to the functional component using the useState
Hook. The useEffect
Hook updates the document title on every render, showcasing the versatility of React Hooks.
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
export default Example;
setup()
method of the Composition API. Unlike React, the setup()
method is executed only once, serving as an entry point to the Composition API. This enables the definition of state without recreating it with each call. The following code illustrates the script part of a Vue component, utilizing watchEffect
and ref
to keep track of a variable (price
) and display its updated value.
<script>
import { watchEffect, ref, defineComponent } from "vue";
export default defineComponent({
name: “Store”,
setup() {
const price = ref(10);
watchEffect(() => console.log(price.value));
return {
price,
};
},
});
</script>
<template>
<h3>Price</h3>
<input v-model=“price” id=“price” placeholder=“edit me”>
</template>
useState
facilitates the creation of reactive objects with a two-member array. In Vue’s Composition API, a similar result is achieved by defining reactive objects inside the setup()
component without the need for a mutation function. The following Vue example showcases the simplicity of defining and updating a reactive object.
<template>
<p>You clicked {{count}} times</p>
<button @click="count += 1">
Click me
</button>
</template>
<script>
import { ref } from “vue”;
export default {
name: “Hooks”,
setup() {
let count = ref(0);
return {
count,
};
},
};
</script>
setup()
default behavior prevents such issues, executing before the component creation. Vue’s Composition API shines in complex scenarios, offering more concise and readable code compared to React Hooks.
// React
const [name, setName = useState('Apple');
const [price, setPrice] = useState(20);
const [quantity, setQuantity] = useState(100);
// Vue
setup () {
return {
name: ref(‘Apple’),
price: ref(20),
quantity: ref(100)
}
}useState
Hook, while Vue utilizes its syntax, including v-model
. The Vue example demonstrates how the value of a local state (name
) changes effortlessly.
// React
import { useState } from 'react';
function Example() {
const [name, setName] = useState(‘Apple’);
return (
<form>
<input
type=“text”
value=name
onChange={e => setName(e.target.value)}
/>
<h2>My favorite fruit is {name}</h2>
</form>
);
}
<!-- Vue -->
<script>
import { ref } from 'vue';
export default {
setup() {
return {
name: ref(‘Apple’),
};
},
};</script>
<template>
<form>
<input type=“text” v-model=“name” />
<h2>My favorite fruit is {{name}}</h2>
</form>
</template>
useEffect
Hook for this purpose, allowing developers to manage various side effects. Vue’s Composition API offers similar capabilities with the watch
and watchEffect
methods, each with its own use cases. The Vue example below demonstrates the usage of watchEffect
to log changes in the price
variable.
<script>
import { watchEffect, ref, defineComponent } from "vue";
export default defineComponent({
setup() {
const price = ref(100);
watchEffect(() => console.log(price.value));
return {
price,
};
},
});
</script>
useEffect
and Vue’s watch
and watchEffect
methods handle side effects, there are key differences in their usage. React’s useEffect
allows manual addition of dependencies, specifying when the effects should be active. Vue’s methods, on the other hand, provide flexibility in tracking changes in variables but require explicit setup.
© 2013 - 2025 Foreignerds. All Rights Reserved