use memo

 =


useMemo in React vs Computed Properties in Vue

  • React’s useMemo caches expensive computations, only recalculating them when dependencies change.
  • Vue’s computed properties serve the same purpose, caching values and recalculating only when their dependencies change.

Example in Vue:

javascript
export default { data() { return { num1: 5, num2: 10 } }, computed: { sum() { console.log("Calculating sum..."); return this.num1 + this.num2; } } }

=


useMemo in React vs. Computed Properties in Vue
Reacts useMemo to memoize expensive calculations:

javascript
Copy code
import { useState, useMemo } from 'react';

function MyComponent() {
  const [num1, setNum1] = useState(5);
  const [num2, setNum2] = useState(10);

  const sum = useMemo(() => {
    console.log("Calculating sum...");
    return num1 + num2;
  }, [num1, num2]);

  return (
    <div>
      <p>Sum: {sum}</p>
    </div>
  );
}
Vue equivalent using computed properties:

javascript
Copy code
export default {
  data() {
    return { num1: 5, num2: 10 };
  },
  computed: {
    sum() {
      console.log("Calculating sum...");
      return this.num1 + this.num2;
    }
  }
}


=

No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...