Urgent.News

What's breaking now, across thousands of outlets.

Tech

Hooked on React: A useState & useEffect Adventure (Like Leveling Up in Zelda)

The Quest Begins (The "Why") Honestly, I remember the first time I tried to build a simple counter in React using a class component. I had a state object, a clunky this.setState call, and then I needed to fetch some data when the component mounted. I slapped a componentDidMount method in there, added a cleanup in componentWillUnmount , and somehow ended up with a weird bug where the count would…

The Quest Begins (The Why)

The author recalls their first attempt at building a simple counter in React using a class component. They struggled with a clunky this.setState call and had to implement componentDidMount and componentWillUnmount methods to fetch data upon mounting. A bug caused the count to jump two steps every time they clicked, leading to frustration as they tried to debug the issue.

This experience left them feeling like they were fighting a final boss with a broken sword, putting in a lot of effort for little payoff. This frustration motivated them to explore hooks, despite initial skepticism about whether they would actually simplify their development process. Little did they know, they were about to discover a spell that would transform their class incantations into elegant, functional incantations.

The Revelation (The Insight)

The turning point occurred when the author realized that useState and useEffect were not just new APIs, but rather a different way of thinking about state and side effects. With useState, they gained a pair: the current state value and a setter function. No more dealing with this.state or this.setState. With useEffect, they learned to tell React to run a function after rendering and optionally clean up when the component unmounts or before the effect runs again.

The dependency array was the key to mastering this concept: it instructed React on exactly when to re-run the effect, preventing those pesky infinite loops. It felt like swapping out a heavy, rickety backpack for a lightweight, modular utility belt. With hooks, they only carried what was necessary for each quest, and they could swap out custom hooks without having to rebuild the entire component. Wielding the Power (Code & Examples)

Before using hooks, the author had a class component with a counter and data fetching functionality. The class included a state object with count, data, and loading properties, as well as componentDidMount, componentDidUpdate, and componentWillUnmount methods. The fetchData method handled aborting any ongoing fetches, setting the loading state to true, and using fetch to retrieve data from an API.

The render method displayed the count, a button to increment it, the loading message if applicable, and the fetched data if available.

After adopting hooks, the author rewrote the CounterWithFetch function. They used the useState hook to manage the count state and the useEffect hook to handle data fetching. The count state was initialized to 0. The useEffect hook ran after the initial render and included a dependency array containing the count state, ensuring that the effect would only run when the count changed.

Inside the effect, they aborted any previous fetches, set the loading state to true, and used fetch to retrieve data from the same API. The render method remained largely unchanged, displaying the count and data as before.

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

This story

This is one outlet's version. Read the fullest account.

Read the original at dev.to →

More in Tech

기술 튜토리얼 게시 전 링크 점검

기술 튜토리얼의 링크 점검과 관리 기술 튜토리얼은 설명이 명확하고 화면 구성이 깔끔해도 링크 품질이 낮으면 전체적인 사용 경험이 크게 달라질 수 있습니다. 설치 과정을 따라가던 독자가 패키지 문서, API 레퍼런스, 설정 안내를 차례로 확인하는 과정에서 오래된 페이지나 로그인 전용 문서를 만나면 흐름이 끊깁니다.

  • Link checking essential before publishing technical tutorials
  • Poor link quality disrupts user experience during installation process
  • Categorize links into official, community, example, background sources

The explanation was right. The policy ID was wrong.

This is a submission for the Kaggle Benchmarking Challenge . What I Benchmarked A support model told me the correct fee, named the applicable policy in its explanation, and then put a different policy…

  • Support model provided correct fee and policy.
  • Policy ID in sourceids field was incorrect.
  • GPT-5.4-mini passed 26 of 30 pairs, Gemini-3.7-flash passed all 15.

How to Convert an Image to Base64 (Data URLs): When It Helps and When It Hurts

A data URL lets you embed a file's contents directly in your HTML or CSS instead of linking to a separate file. Images encoded as Base64 are the classic example.

  • Data URLs embed file contents directly in HTML/CSS, saving a network request.
  • To convert an image to Base64, use a browser tool and paste the full string into src or url().
  • Base64 increases image size by ~33%, can't cache separately, and bloats HTML/CSS.

Ankita is looking for contributors — here's what you'd actually build

I'm Krish, a developer from India. For the past 8 months I've been building Ankita — a free, open-source desktop AI assistant. Terminal + Electron app, plugin/skill system, local-first by design.

  • Krish developed free, open-source desktop AI assistant Ankita over 8 months
  • App features terminal, Electron, plugin system, local-first design
  • Krish seeks contributors to build real-world Node.js and Electron app

More from Thursday 1 October →