Official Solution
import React, { useMemo, useState } from 'react';
const data = Array.from({ length: 120 }, (_, i) => ({ id: i + 1, title: 'Question ' + (i + 1) }));
export default function App() {
const [q, setQ] = useState('');
const [count, setCount] = useState(0);
const filtered = useMemo(() => {
const query = q.trim().toLowerCase();
if (!query) return data;
return data.filter((x) => x.title.toLowerCase().includes(query));
}, [q]);
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode list</h2>
<input value={q} onChange={(e) => setQ(e.target.value)} placeholder='Filter' style={{ width: '100%', padding: '10px 12px', borderRadius: 12, border: '1px solid #bbb' }} />
<button type='button' onClick={() => setCount((n) => n + 1)} style={{ marginTop: 10, padding: '8px 12px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Re-render ({count})</button>
<div style={{ marginTop: 10, color: '#555' }}>Showing {filtered.length} items</div>
<ul style={{ marginTop: 10, paddingLeft: 18, maxHeight: 220, overflow: 'auto', border: '1px solid #eee', borderRadius: 14, padding: 12 }}>
{filtered.slice(0, 30).map((x) => <li key={x.id}>{x.title}</li>)}
</ul>
</div>
);
}
No comments yet. Start the discussion!