import React, { useCallback, useRef, useState } from 'react'; import debounce from 'lodash/debounce'; import { Mentions } from 'antd'; const App: React.FC = () => { const [loading, setLoading] = useState(false); const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]); const ref = useRef(); const loadGithubUsers = (key: string) => { if (!key) { setUsers([]); return; } fetch(`https://api.github.com/search/users?q=${key}`) .then((res) => res.json()) .then(({ items = [] }) => { if (ref.current !== key) return; setLoading(false); setUsers(items.slice(0, 10)); }); }; const debounceLoadGithubUsers = useCallback(debounce(loadGithubUsers, 800), []); const onSearch = (search: string) => { console.log('Search:', search); ref.current = search; setLoading(!!search); setUsers([]); debounceLoadGithubUsers(search); }; return ( ({ key: login, value: login, className: 'antd-demo-dynamic-option', label: ( <> {login} {login} ), }))} /> ); }; export default App;