This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import useDebounce from './useDebounce'; | |
const Search = () => { | |
const [searchTerm, setSearchTerm] = useState(''); | |
const [results, setResults] = useState([]); | |
// ✅ Use debounce hook to debounced searchTerm as it is rapidly changing | |
const debouncedSearchTerm = useDebounce(searchTerm, 500); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
const useDebounce = <T>(value: T, delay: number): T => { | |
const [debouncedValue, setDebouncedValue] = useState(value); | |
useEffect(() => { | |
const handler = setTimeout(() => { | |
setDebouncedValue(value); | |
}, delay); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
const useWhenVisible = (target: Element | undefined, | |
callback: () => void, | |
root: Element | undefined = document.body) => { | |
useEffect(() => { | |
if (!target || !root) { | |
return; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState, useRef } from 'react'; | |
import useWhenVisible from './useWhenVisible'; | |
const TodoList = () => { | |
const limit = 25; | |
const [offset, setOffset] = useState(0); | |
const [todos, setTodos] = useState([]); | |
const lastEl = useRef(); | |
useEffect(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useRef } from 'react'; | |
const useTimeout = (callback: () => void, delay: number | null) => { | |
const savedCallback = useRef(callback); | |
useEffect(() => { | |
savedCallback.current = callback; | |
}, [callback]); | |
useEffect(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useRef } from 'react'; | |
const useInterval = (callback: () => void, delay: number | null) => { | |
const savedCallback = useRef(callback); | |
useEffect(() => { | |
savedCallback.current = callback; | |
}, [callback]); | |
useEffect(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useCallback } from 'react'; | |
import useInterval from './useInterval'; | |
const ShoppingList = () => { | |
// Wait 5 seconds before fetching new data | |
const POLL_DELAY = 5000; | |
const [items, setItems] = useState([]); | |
const fetchItems = useCallback(async () => { | |
const response = await fetch('/shopping-list/items'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Todo = { | |
id: number; | |
title: string; | |
completed: boolean; | |
} | |
const todos: Todo[] = [ | |
{ id: 1, title: 'Buy nachos', completed: false }, | |
{ id: 2, title: 'Buy avocado', completed: true }, | |
{ id: 3, title: 'Buy ground beef', completed: false }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Todo = { | |
id: number; | |
title: string; | |
completed: boolean; | |
} | |
const todos: Todo[] = [ | |
{ id: 1, title: 'Buy nachos', completed: false }, | |
{ id: 2, title: 'Buy avocado', completed: true }, | |
{ id: 3, title: 'Buy ground beef', completed: false }, |
OlderNewer