Urgent.News

What's breaking now, across thousands of outlets.

Tech

Your Custom Hooks Tear in React 18. `useSyncExternalStore` Fixes It.

The header says "You're offline." Three pixels below it, the save button is still clickable, sending requests like nothing's wrong. Same navigator.onLine value. Same render. Two different answers on screen at the same time. You didn't write a new bug. You wrote the hook everyone writes. function useOnlineStatus () { const [ online , setOnline ] = useState ( navigator . onLine ); useEffect (() =>…

React 18 introduced `useSyncExternalStore`, a fix for the tearing issue caused by concurrent rendering. The hook `useOnlineStatus()` was fine in previous versions, but when wrapped in `startTransition`, it started producing inconsistent results. This happens because `useState` + `useEffect` assumes that once a component commits, it stays committed until the next explicit update, and that all components re-render from the same world. However, concurrent rendering breaks this assumption.

`useSyncExternalStore` allows you to subscribe to external state, such as browser APIs, without running into tearing. It takes three functions: a callback to subscribe to changes, a function to get the current snapshot value, and an optional function for server-side rendering. The key difference is that `getSnapshot` is called every render, and React checks the current value synchronously before committing, effectively closing the gap where tearing occurs.

You may already be using `useSyncExternalStore` in libraries like Zustand or react-redux, but if you have your own custom hooks, consider using this hook for any state subscriptions that live outside of React. Remember to include the optional `getServerSnapshot` function when rendering on the server to avoid hydration mismatches.

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

Replacing Excel with a Next.js Web App: A Dev's Playbook

Every dev has seen it: a business running its entire operation on a 40-tab Excel file named Orders_FINAL_v2_REAL.xlsx. The 'Excel person' holds it all in their head, formulas break silently, and…

  • AimEncoders developed playbook to replace Excel with modern web apps
  • Data modeling foundation for simplified development process
  • PostgreSQL with row-level permissions ensures reliable operation

Day 22 of My Web Development Journey 🚀 | Meta Tags and Introduction to React.js

Today, I learned about HTML meta tags and started exploring React.js. I also learned the difference between a library and a framework and how to set up a React project using the terminal. 1.

  • Meta tags provide webpage information for browsers and search engines
  • React.js is a JavaScript library for building interactive web apps
  • React.js is a library, not a framework, offering reusable components

The .pth file that runs before your code: lessons from the LiteLLM 1.107.0 compromise

The .pth file that runs before your code: lessons from the LiteLLM 1.107.0 compromise Version 1.107.0 of the LiteLLM Python package was published with a credential stealer and a backdoor inside it.

  • .pth file executes Python code before application import, posing security risk
  • LiteLLM 1.107.0 compromise delivered credential stealer and backdoor via compromised GitHub accounts
  • Malicious .pth file triggered credential collector at interpreter start-up

More from Friday 23 October →