Code RoomToken count lags behind text
HardPrep Room Coding #2035

Token count lags behind text

Code reviewCode quality & reviewSenior–Staff~20 min

Review this React TypeScript token-streaming reducer.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 20 min
Mark a line and say what kind of problem it is.0 findings
1function Stream() {
2 const [text, setText] = useState('');
3 const [tokens, setTokens] = useState(0);
4 
5 const onChunk = (chunk: string) => {
6 setText(text + chunk);
7 setTokens(tokens + chunk.split(' ').length);
8 };
9 
10 useSocket('/stream', onChunk);
11 
12 return <pre>{text} ({tokens} tokens)</pre>;
13}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.