<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shimrit Breef Ziskand</title>
    <description>The latest articles on DEV Community by Shimrit Breef Ziskand (@shimritz).</description>
    <link>https://dev.to/shimritz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1123231%2F825255e2-3d32-45a4-a8b6-ab4a38eedb99.png</url>
      <title>DEV Community: Shimrit Breef Ziskand</title>
      <link>https://dev.to/shimritz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shimritz"/>
    <language>en</language>
    <item>
      <title>State Changes In React.js and Vue.js</title>
      <dc:creator>Shimrit Breef Ziskand</dc:creator>
      <pubDate>Sat, 22 Jul 2023 11:34:06 +0000</pubDate>
      <link>https://dev.to/shimritz/state-changes-in-reactjs-and-in-vuejs-3em2</link>
      <guid>https://dev.to/shimritz/state-changes-in-reactjs-and-in-vuejs-3em2</guid>
      <description>&lt;p&gt;Recently I have got a chance to work with Vue.js framework and found some familiar concepts between Vue and React.&lt;/p&gt;

&lt;p&gt;Both frameworks provide reactive state management and while the concept is the same the semantics are quite different.&lt;/p&gt;

&lt;p&gt;In React we would use &lt;code&gt;useEffect&lt;/code&gt; hook while Vue provides us with &lt;code&gt;watch directive&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both of those (&lt;em&gt;&lt;code&gt;watch directive&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;&lt;/em&gt;) watch for changes made to internal component properties.&lt;br&gt;
However, there are some key differences between those two.&lt;/p&gt;

&lt;p&gt;It is good to remember that &lt;code&gt;watch directive&lt;/code&gt; in not tied to component lifecycle unlike React's &lt;code&gt;useEffect&lt;/code&gt; that is fully aware of component's lifecycle (mount, unmount, update) &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;watch directive&lt;/code&gt; can only be used to watch for changes to properties, while &lt;code&gt;useEffect&lt;/code&gt; can also be used to perform side effects, such as fetching data or updating the DOM.&lt;/p&gt;

&lt;p&gt;Let’s learn about both by using a code example.&lt;/p&gt;

&lt;p&gt;This is example of how watcher works in Vue.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div id="app"&amp;gt;
    &amp;lt;p&amp;gt;Counter: {{ counter }}&amp;lt;/p&amp;gt;
    &amp;lt;button @click="increment"&amp;gt;Increment&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data () {
    return {
      counter: 0
    }
  },
  watch: {
    counter (newValue, oldValue) {
      console.log('Counter changed from', oldValue, 'to', newValue)
    }
  },
  methods: {
    increment () {
      this.counter++
    }
  }
}
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will create a simple counter that will log a message to the console whenever the counter value changes. The watch directive is used to register a callback function that will be called whenever the counter property changes. The callback function will be passed two arguments: the new value of the property and the old value of the property.&lt;/p&gt;

&lt;p&gt;The watch directive takes a property name as its argument, and then a callback function as its value. The callback function will be called whenever the property changes. In this case, the counter property is being watched, and the callback function will log a message to the console whenever the counter value changes.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;React&lt;/strong&gt;, we use the built-in &lt;code&gt;useEffect&lt;/code&gt;hook to watch for changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from "react";

function Counter() {
  const [counter, setCounter] = useState(0);

  useEffect(() =&amp;gt; {
    console.log("Counter changed to", counter);
  }, [counter]);

  function increment() {
    setCounter(counter + 1);
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Counter: {counter}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is very similar to the Vue.js example, but instead of using the &lt;code&gt;watch directive&lt;/code&gt;, we are using the &lt;code&gt;useEffect&lt;/code&gt; hook to watch for changes to the counter state variables. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook takes a callback function as its first argument. The callback function will be called whenever the component is mounted or whenever any of the dependencies change. &lt;br&gt;
The dependencies (she second argument of the &lt;code&gt;useEffect&lt;/code&gt; hook) can contain any value that can be compared with ===. &lt;br&gt;
This includes primitives, objects, and functions (in our case it's a primitive (number) value ===&amp;gt; [counter]).&lt;/p&gt;

&lt;p&gt;If you do not pass any dependencies to the &lt;code&gt;useEffect&lt;/code&gt; hook, the hook will only be run &lt;strong&gt;once&lt;/strong&gt; - on component mount.&lt;/p&gt;

&lt;p&gt;In this case, the callback function logs a message to the console whenever the counter state variable changes.&lt;/p&gt;

&lt;p&gt;In conclusion, I have introduced the similarities and differences between the &lt;code&gt;useEffect&lt;/code&gt; hook in React and the watch directive in Vue. &lt;br&gt;
I hope that this example has illustrated how both are useful and powerful tools.&lt;/p&gt;

</description>
      <category>react</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
