useThread.tslive

Write the hook.
Ship the insight.

Thread is where React engineers untangle gnarly logic at 2am — and wake up to 47 upvotes on the answer.

useThread.ts
1import { useCallback, useEffect, useRef, useState } from 'react'
2
3export function useThread(threadId: string) {
4 const [replies, setReplies] = useState<Reply[]>([])
5 const wsRef = useRef<WebSocket | null>(null)
6
7 useEffect(() => {
8 wsRef.current = new WebSocket(
9 `wss://thread.dev/t/${threadId}`
10 )
11
12 return () => wsRef.current?.close()
13 }, [threadId])
14
15 return { replies, postReply, isLoading }
2,841active threads
394replies today
127hooks shipped
#hooks·3 replies · 2 min ago

useEffect cleanup not firing on WebSocket reconnect — memory leak in prod?

84 views
PM
Priya MehtaCore Team2m ago

Your cleanup function needs to close the socket before reassigning — you're leaking connections on every `threadId` change.

wsRef.current?.close()
wsRef.current = new WebSocket(...)
MW
Marcus WebbOSS Maintainer8m ago

Also worth adding a `readyState` guard — `WebSocket.OPEN` check before you send. Silent failures in prod are brutal.

SP
Soo-Jin Park14m ago

I'd extract this into a `useWebSocket` primitive and compose it here. Keeps the thread logic clean. See my gist ↗

Topic Explorer

Hover a topic.
Watch the community surface.

Recent in #use-effect-patterns3 threads
hot

The cleanup function you keep forgetting to write

@mwebb_oss·89 replies
312

Abort controllers in useEffect: the complete pattern

@soojin_fe·44 replies
178

StrictMode double-invoke: feature or footgun?

@lena_arch·71 replies
234
Community Stats
◈
2,103
Members
◎
134
Posts this week
◆
@mwebb_oss
Top contributor
◇
23
Open issues
Activity — last 7 days
MTWTFSS
Join this channel →
The Community

The engineers
already inside.

OSS maintainers, staff engineers, and mid-level devs who actually reply. Mentorship matching opens weekly.

MW
Core Team
Marcus Webb
@mwebb_oss
OSS Maintainer

Maintains 3 React libraries. Will argue about state management until 4am.

react-queryzustand
312threads
4,820reputation
PM
Mentor
Priya Mehta
@priya_dev
Senior Frontend Architect

Performance obsessive. Profiler tab is always open. Ships to prod daily.

reactvite
287threads
3,940reputation
SP
Soo-Jin Park
@soojin_fe
Frontend Engineer

Atomic state evangelist. Every piece of state should earn its place.

jotaivaltio
198threads
2,760reputation
LH
Mentor
Lena Hoffmann
@lena_arch
Staff Engineer

Monorepo whisperer. Built and migrated 4 Next.js apps from Pages to App Router.

next.jsturborepo
234threads
3,310reputation
TK
Core Team
Tobias Krause
@tobias_k
TypeScript Specialist

If it can be typed, it will be typed. Generic constraints are a love language.

typescriptzod
276threads
3,580reputation
AT
Adaeze Thornton
@ada_t
React Performance Lead

Profiler. Bundle analyzer. The one who files the issue before you even notice the bug.

react-devtoolswhy-did-you-render
189threads
2,890reputation
NO
Nnamdi Okafor
@dev_nnamdi
Full-Stack React Dev

tRPC advocate. Believes the best API is the one you never have to document.

trpcprisma
156threads
2,240reputation
RS
Ravi Shankar
@ravi_hooks
Mid-Level Frontend Engineer

Debugging useEffect since React 16. Getting better at it, slowly.

react-hook-formreact-table
134threads
1,870reputation
Mentorship Matching — Open Now

Senior engineers open to pairing this week: 12 mentors available. Match expires Sunday.

Apply for a mentor →
One click away

The cursor is
already blinking.

2,841 threads. 8,400 React engineers. Zero recruiters. Sign in with GitHub — one click, you're in.

⚡No email required
🔒GitHub OAuth only
∅Zero recruiters
⌘Free forever