Urgent.News

What's breaking now, across thousands of outlets.

Tech

React.js ~The Latest Ref Pattern ~

This pattern holds callback passed to a customhook in the ref and update existing value everytime rendered to have the latest value. function useDebounce ( callback , delay ) { const callbackRef = React . useRef ( callback ) React . useLayoutEffect (() => { callbackRef . current = callback }) return React . useMemo ( () => debounce ((... args ) => callbackRef . current (... args ), delay ), […

The latest Ref pattern in React is a technique that allows you to pass a callback to a custom hook and update its value every time the component is rendered, ensuring you always have the most recent value. This pattern was introduced by Yago Pereria.

The main reason for using the Ref is to maintain a current value without triggering a re-render, which would otherwise lead to an infinite loop. By holding values in state, the component re-renders every time the value changes, causing an endless loop.

In the second part of the article, the focus is on dealing with Dependency Arrays. While it's necessary to follow the exhaustive-deps rule, you should not include the current value of a Ref in the Dependency Array. Since updating the Ref does not trigger a re-render, React cannot detect any changes to the current value, leading to issues in debugging.

React 19 introduced the useEffectEvent, which is specifically designed to incorporate this pattern into the official API. With React 19, there might be situations where creating a handwritten "latest Ref" is no longer required.

Callbacks in React are updated every time the component renders, as a new instance of the function is created with each render. This means that even if the function appears only once in the source code, a new instance is created with every render.

A common example is a debounce function that filters API calls based on user input. Although the arrow function appears only once in the source code, a new instance is created every time the component is rendered. As query changes with every input, it effectively becomes a "different function" with every keystroke. If you hold onto the function generated in the first render, the function that runs is the function that watches the initial query and keeps searching for an empty string, causing a classic "stale closure" bug.

Instead of adding the callback to the Dependency Array, which would cause the debounced instance to be recreated and discard pending timers, using a Ref is a better approach. This way, you can maintain both a stable instance of debounce and an up-to-date function.

In summary, the latest Ref pattern is a useful technique in React for updating values without triggering unnecessary re-renders while ensuring that callbacks remain current. This pattern is particularly helpful when dealing with Dependency Arrays and managing stale closures.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Never silently change a number someone gets paid on

Our algorithm decided a driver had travelled 4km less than they thought. It was probably right. Shipping that silently would still have been wrong.

  • Algorithm may adjust mileage recorded by drivers
  • Transparency required: show original vs. adjusted distance
  • Drivers should override algorithm decisions if needed

What breaks when you self-host a TURN server (coturn — symptom, cause, fix)

The first article in this series was the traps inside the code. The second was the layer underneath — the candidates that lie, the UDP firewall nobody looks at, the reverse proxy that reaps an idle…

  • TURN server issues may stem from misdiagnosing relay as solution
  • Verify coturn service and port configuration for proper operation
  • Open relay port range in firewalls for TURN port allocation

How I Built Authentication in Go and React: Doing It Right

Every tech blog will tell you the same thing: don’t roll your own auth. It makes sense not to do that for production applications with a large number of users, where more is at stake, and to use…

  • Author built authentication system in React, Go, and PostgreSQL for learning purposes
  • Chose JWTs for stateless verification, set 15-minute expiry to limit validity
  • Stored refresh tokens in HttpOnly, Secure cookies to prevent XSS and MITM attacks

What I’ve Been Building While Looking for My Next Opportunity

For a while now, I’ve been wondering what it actually takes to land an international role, especially one that could eventually lead to relocation. I’ve thought about it a lot.

  • Author builds SaaS web application (Flowwbook) for appointment-based businesses.
  • Develops AI-powered EdTech mobile app for exam preparation (JAMB, WAEC, NECO).
  • Seeks full-time Product Design role or contract opportunity in international product teams.

More from Sunday 20 September →