Open ahhzaky opened 4 years ago
use HOOKS.
const CONTEXT_KEY = "FROM GOOGLE programmable search";
function useGoogleSearch(term) { const [data, setData] = useState(null);
useEffect(() => { const fetchData = async () => { fetch( https://www.googleapis.com/customsearch/v1?key=${API_KEY}&cx=${CONTEXT_KEY}&q=${term} ) .then((response) => response.json()) .then((result) => { setData(result); }); };
https://www.googleapis.com/customsearch/v1?key=${API_KEY}&cx=${CONTEXT_KEY}&q=${term}
fetchData();
}, [term]);
return { data }; }
export default useGoogleSearch;
2:24:00 from video
use HOOKS.
const CONTEXT_KEY = "FROM GOOGLE programmable search";
function useGoogleSearch(term) { const [data, setData] = useState(null);
useEffect(() => { const fetchData = async () => { fetch(
https://www.googleapis.com/customsearch/v1?key=${API_KEY}&cx=${CONTEXT_KEY}&q=${term}
) .then((response) => response.json()) .then((result) => { setData(result); }); };}, [term]);
return { data }; }
export default useGoogleSearch;