# State Update is Asynchronous (slower than what you expect!)

`const handleChange = (event) => {  setInput(event.target.value);  fetchData(input);              // x , prev input value  fetchData(event.target.value); // o , current value};`
`useEffect(()=>{  console.log(input)             // o , current input value },[input])const handleChange = (event) => {  setInput(event.target.value);  fetchData(input);              // x , prev input value  fetchData(event.target.value); // o , current input value};`
`useEffect(()=>{  async function fetchData(input) {...}  const data = await fetchData()  setData(data)},[input])const handleChange = (event) => {  setInput(event.target.value)}`

# Debounce and Re-render

`function MySearch() {  const [input, setInput] = useState('')  // create memoized version of debounced data fetch function  const debouncedFetchData = useCallback(debounce(fetchData, 500),   []);  // call memoized debounced function in the event handler so the   intermediate searches are cancelled by debounce while the user   types  const handleChange = (event) => {    setInput(event.target.value);    debouncedFetchData(event.target.value);  };  return <input onChange={handleChange} value={input} />}`
{"ft_lang_label":"__label__en","ft_lang_prob":0.7508236,"math_prob":0.9421083,"size":4348,"snap":"2022-27-2022-33","text_gpt3_token_len":941,"char_repetition_ratio":0.14410682,"word_repetition_ratio":0.057902973,"special_character_ratio":0.2127415,"punctuation_ratio":0.1331558,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97228354,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-18T11:37:36Z\",\"WARC-Record-ID\":\"<urn:uuid:4bbdb289-4056-4587-b87a-fd411343af1e>\",\"Content-Length\":\"156976\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:db67ee4f-fc04-4757-be0c-bd35aecbdeab>\",\"WARC-Concurrent-To\":\"<urn:uuid:467763fd-342e-440e-aa44-a8c5f151485c>\",\"WARC-IP-Address\":\"162.159.152.4\",\"WARC-Target-URI\":\"https://sherryhsu.medium.com/react-async-data-fetch-f5ccb107d02b?readmore=1&source=user_profile---------2-------------------------------\",\"WARC-Payload-Digest\":\"sha1:ISABSY3OC6JARMWTDSHIWW4DUNJ7FERS\",\"WARC-Block-Digest\":\"sha1:CFZ5H3RBJ4UXJDBYQX3TLUHZTOJA23IU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882573193.35_warc_CC-MAIN-20220818094131-20220818124131-00423.warc.gz\"}"}