OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is React not updating the reference variable via assignment operator but updating via shorthand operator? [closed]

  • Thread starter Thread starter boggy
  • Start date Start date
B

boggy

Guest
I was trying to see how many times the code re-render and hence used a reference Hook to check it. Though the reference variable was updating when I was using shorthand operator it was not working using assignment operator and is printing NaN.


Code:
const { useState, useRef } = React;

function Stopwatch() {
  const [startTime, setStartTime] = useState(null);
  const [now, setNow] = useState(null);
  const ref = useRef(0);

    //ref.current=ref.current+1;
      ref.current+=1;
  function handleStart() {
    // Start counting.
    setStartTime(Date.now());
    setNow(Date.now());

    setInterval(() => {
      // Update the current time every 10ms.
      setNow(Date.now());
    }, 10);
  }

  let secondsPassed = 0;
  if (startTime != null && now != null) {
    secondsPassed = (now - startTime) / 1000;
  }

  return (
    <div>
      <h1>Time passed: {secondsPassed.toFixed(3)}</h1>
      <button onClick={handleStart}>
        Start
      </button>
      <p>{ref.current}</p>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<Stopwatch />);

Code:
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>

Anything I am missing while assigning this?

<p>I was trying to see how many times the code re-render and hence used a reference Hook to check it. Though the reference variable was updating when I was using shorthand operator it was not working using assignment operator and is printing NaN.</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>const { useState, useRef } = React;

function Stopwatch() {
const [startTime, setStartTime] = useState(null);
const [now, setNow] = useState(null);
const ref = useRef(0);

//ref.current=ref.current+1;
ref.current+=1;
function handleStart() {
// Start counting.
setStartTime(Date.now());
setNow(Date.now());

setInterval(() => {
// Update the current time every 10ms.
setNow(Date.now());
}, 10);
}

let secondsPassed = 0;
if (startTime != null && now != null) {
secondsPassed = (now - startTime) / 1000;
}

return (
<div>
<h1>Time passed: {secondsPassed.toFixed(3)}</h1>
<button onClick={handleStart}>
Start
</button>
<p>{ref.current}</p>
</div>
);
}

ReactDOM.createRoot(document.getElementById("root")).render(<Stopwatch />);</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script></code></pre>
</div>
</div>
</p>
<p>Anything I am missing while assigning this?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top