frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

376 - Stale Refs - react

Open jsartisan opened this issue 2 months ago • 0 comments

App.jsx

import { useState, useRef, useEffect } from "react";

export default function App() {
  const [value, setValue] = useState();

  const ref = useRef(() => {
    console.log("Submitted value:" + value);
  });

  useEffect(() => {
    ref.current = () => {
      console.log("Submitted value:" + value);
    }
  }, [value])

  return (
    <>
      <input onChange={(e) => setValue(e.target.value)} />
      <button onClick={() => ref.current()}>Submit</button>
    </>
  );
}

jsartisan avatar Oct 03 '25 06:10 jsartisan