Skip to content

Instantly share code, notes, and snippets.

View yilmazcakmakci's full-sized avatar

Yılmaz Çakmakçı yilmazcakmakci

View GitHub Profile
@yilmazcakmakci
yilmazcakmakci / useFetch.js
Created August 9, 2021 09:55
A React hook for handle different states on async requests without useState in component.
import { useEffect, useState, useCallback } from 'react'
const useFetch = (fetcher, { params = {}, immediate = false } = {}) => {
const [loading, setLoading] = useState(false)
const [data, setValue] = useState(null)
const [error, setError] = useState(null)
const execute = useCallback(
(executeParams) => {
setLoading(true)
@yilmazcakmakci
yilmazcakmakci / useQuery.js
Created April 29, 2021 14:42
Get query of the current location with key-value pairs
const useQuery = () => {
const url = new URL(window.location.href)
const params = new URLSearchParams(url.search)
const query = {}
for(const pair of params.entries()) {
query[pair[0]] = pair[1]
}
return query