Skip to main content

10 posts tagged with "react"

Posts about React

View All Tags

Difference between useState and useMemo

· 2 min read
Filip Tammergård
Software Engineer at Frilans Finans

The title of this post might seem ridiculous. Of course useState and useMemo are different.

But in one sense, they are quite similar.

Consider this example:

const [state] = useState(() => {
return calculateSomething(input)
})

const memo = useMemo(() => {
return calculateSomething(input)
}, [input])

Building a generic table in React and TypeScript

· 4 min read
Filip Tammergård
Software Engineer at Frilans Finans

I want to take you with me on a journey of creating a table component that is generic and statically typed. Instead of just showing an end result, I want to try out different approaches that have different pitfalls, and find the solution that seems to be the best one.

As a starting point, we want to render a table with some country data:

CountryCapitalDate formatInternet TLD
SwedenStockholmYYYY-MM-DD.se
GermanyBerlinDD.MM.YYYY.de
BrazilBrasíliaDD/MM/YYYY.br

Custom hook: useClipboard

· One min read
Filip Tammergård
Software Engineer at Frilans Finans

Some time ago I wrote a blog post on how to access the clipboard with the navigator API. I decided to make it a custom hook to make it super easy to use. Before we go into code, let's try it out — click the button to read your clipboard:

Nothing to show...

Pi decimals

· One min read
Filip Tammergård
Software Engineer at Frilans Finans

How many decimals of pi do you know? Here you can test yourself and try to improve. Click the box below and type the first decimal to begin. If you want help, you can click the "Show answer" button, where you'll also see how far you've come — the correctly typed decimals have a green background.

Disco generator

· 3 min read
Filip Tammergård
Software Engineer at Frilans Finans

It's Friday disco time!

TL;DR

  • A grid of cells each get a random rgb(...) color, and the useInterval custom hook reassigns every color on each beat.
  • The frequency in hertz maps to the interval delay as 1000 / frequency; dropping below 1 Hz passes null and pauses the disco.
  • The CSS grid layout and each cell's color are set with inline styles, which suit values that change on every render better than CSS classes.

Custom hook: useInterval

· 4 min read
Filip Tammergård
Software Engineer at Frilans Finans

setInterval runs a callback at a fixed cadence. Using it in React means remembering to call clearInterval on unmount, and reaching for extra state any time the interval should pause or restart. This post wraps both concerns in a small custom hook called useInterval.

TL;DR

  • useEffect starts the interval and returns a cleanup that calls clearInterval — no manual unmount handling.
  • The latest callback is kept in a ref, so the interval keeps ticking at a steady rate even when the component re-renders for unrelated reasons.
  • Passing null as the delay pauses the interval, so you can derive "running" straight from props or state instead of tracking it separately.
  • Changing the delay restarts the interval automatically, so a dynamic value "just works".

What have I copied?

· 3 min read
Filip Tammergård
Software Engineer at Frilans Finans

Have you ever copied something and immediately forgotten what it was?

TL;DR

  • navigator.clipboard.readText() returns a Promise<string> with the current clipboard text.
  • It is only available in a secure context (HTTPS or localhost), and the read has to happen during a user activation — that's why it's wired up to a button click rather than running on mount.
  • The call can reject (permission denied, document not focused, no clipboard support, …), so it always belongs in a try/catch.

Letter game

· 4 min read
Filip Tammergård
Software Engineer at Frilans Finans

Start the game by clicking the box and typing the letter shown. The clock starts and you get a new random letter to type as fast as you can. Each correct letter is followed by a new random letter, and if you type the wrong letter, the game ends. Go for it!

TL;DR

  • A small typing game built in React with TypeScript.
  • useReducer keeps the game state in one place, with the current mode as idle or running.
  • useEffect reads the saved high score from local storage and picks the first random letter on mount.
  • A custom useInterval hook drives the running clock.