Sobes.tech
Junior — Senior

Custom useRef hook

livecode

Task condition

Create your own implementation of the useRef hook, mimicking its behavior in React. You need to write a function that returns an object with a current field, allowing you to store a mutable value between renders without recalculating it. When the hook is called, a stable object reference should be maintained throughout the component's lifecycle.

Requirements:

  • The function should work without using the built-in useRef from the React library.
  • The returned object should have a current property that can be read and modified.
  • On subsequent calls within the same component, the reference to the object should remain unchanged.
  • The implementation can be tested in a simple functional component, verifying that the value persists between renders.

Example usage (for illustration, code should not be changed):

function MyComponent() {
  const ref = useMyRef(0);
  // ...
}