Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Learn React Hooks (and Stop Fighting useEffect)

The first React app I wrote with hooks had a useEffect that fetched a user profile. It worked on the first load. Then I added the user ID to the dependency array because the linter told me to, and the network tab filled up with the same request, over and over, until the tab froze. I fixed it by deleting the dependency array. That was the wrong fix, and it took me an embarrassingly long time to…

When you first started writing a React app using hooks, you encountered a problem with an `useEffect` that fetched a user profile. After adding the user ID to the dependency array, the same request kept firing, causing the network tab to freeze. You fixed it by removing the dependency array, but later realized this was the wrong approach.

To truly understand hooks, start by grasping what a render is. Every time state changes, React calls your component function again, creating fresh variables and functions. This is why logging a stale value appears when `count` belongs to a previous render.

Next, learn the updater form of `useState` early, as it's frequently used. If the new state depends on the old one, pass a function to `setCount` to ensure the correct increment. Understanding that objects in state are replaced, not merged, is also crucial.

`useEffect` is employed to sync your component with things outside React's control, like network requests or timers. The dependency array isn't just a list of when to run; it's a list of all values from the render that the effect reads. Lying with it leads to stale data. To avoid this, move functions inside effects, depend on primitive values, or question whether an effect is necessary at all.

Review common mistakes such as looping, stale data, and unnecessary effects. Reviewing these concepts over time through spaced repetition can help solidify your understanding. Consider using tools like Recl, an AI learning app, to save, summarize, and review content for better retention.

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

Ofrenda: A Remembrance Builder That Never Leaves Your Laptop (clear, and states the open-source angle right away)

What I Built Every family has a few people whose stories only exist in someone’s head. A grandmother who hummed while she cooked. An uncle with one phrase he said at every dinner.

  • Ofrenda is an open-source tool for creating personalized remembrances.
  • Users input details about loved ones, which a local Gemma model uses to generate drafts.
  • Completed remembrances are exported as standalone HTML files with photos and animated candles.

Fintech Ticket Triage with Node.js RAG: PDF Upload, Semantic Search, and Citations

Short answer: use Node.js for the upload workflow, pgvector for metadata-aware semantic search, and embeddings plus citations as a validated contract; choose a shared OpenAI-compatible runtime only…

  • PDF uploads divided into overlapping chunks for semantic search
  • Embedded chunks with filename, page, and section metadata
  • Generated answers include citations and provenance for verification

Day 15 of 30: the halfway map, fourteen ideas worth keeping

Halfway through the series. Instead of a self-congratulating check-in, here's something more useful: a map of the fourteen ideas so far, each in a paragraph, so you can skim the series in five minutes…

  • Day 15 marks halfway point in 30-day series
  • Focus on fourteen ideas for safe defaults and systems
  • Next topics: Three.js, Row Level Security, test plan

Tool Selection at Scale: What Top-K RAG Leaves Out

Embed, retrieve, let the LLM decide works for about 100 tools. At 1,000 it needs a control plane: hybrid retrieval, hard filters, a learned reranker, progressive disclosure and per-stage evals.

  • Tools have side effects beyond simple category classification
  • Retrieval controls context and improves accuracy
  • Recall chain is product of conditional recalls at each stage

More from Sunday 1 November →