Skip to content

Instantly share code, notes, and snippets.

View ever-dev's full-sized avatar
🏠
Working from home

Ever Dev ever-dev

🏠
Working from home
View GitHub Profile
@ever-dev
ever-dev / useDebounce.ts
Created August 21, 2020 10:21
useDebouce Custom Hook
import { useState, useEffect } from "react";
export const useDebounce = (value: any, delay: number) => {
// State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(
() => {
// Update debounced value after delay
const handler = setTimeout(() => {
@ever-dev
ever-dev / useTimer.ts
Last active August 21, 2020 15:28
useTimer Custom Hook
import { useState, useEffect } from "react";
export const useTimer = (delay: number) => {
const [clock, setClock] = useState(false);
useEffect(
() => {
// toggle clock
const handler = setInterval(() => {
setClock((v) => !v);
@ever-dev
ever-dev / httpClient.ts
Created August 21, 2020 15:29
Http Client to fetch urls
type THttpMethod = "GET" | "POST" | "PUT" | "DELETE";
export const httpClient = (
url: string,
method: THttpMethod = "GET",
data: any = {}
) => {
const params: { [i: string]: any } = {
method,
headers: {
@ever-dev
ever-dev / promise-in-arrays.js
Created September 18, 2020 08:20
Promise in Arrays
function promiseFunction(v) {
return new Promise((resolve) => {
setTimeout(() => resolve(v), 1000);
});
}
// f1() will print `all done` first and then print 1,2,3,4 at once after a second.
function f1() {
[1, 2, 3, 4]. forEach(async(i) => {
const v = await promiseFunction(i);
@ever-dev
ever-dev / Convert callback to promise.md
Last active September 18, 2020 08:24
The way to make a callback function to Promise.

Use of callback like functions

function someFunction1( callbackFn ) => someFunction1(() => { doSomething(); })
function promiseFunction() {
  return new Promise((resolve) => {
     someFunction1(resolve);
  });
}
@ever-dev
ever-dev / tab.tsx
Last active September 23, 2020 19:10
React Tab Component using emotion
/** @jsx jsx */
import { jsx, css } from '@emotion/core'
import { FunctionComponent, useState } from 'react'
import { Icon } from '~/components'
import PerfectScrollbar from 'react-perfect-scrollbar'
import 'react-perfect-scrollbar/dist/css/styles.css'
interface Tab {
title: string
Content: FunctionComponent<{}>
@ever-dev
ever-dev / React Components with initalValue.md
Last active September 25, 2020 10:37
Regarding the components with initialValue prop

We often face (or sometimes we need to implement) initialValue which sets the value only for the first time when it's rendered. (Fortunately, I had a chance to implement initialValue not so long ago).
A good example is formik.

const [types, setTypes] = useEffect(null);
const getTypes = useCallback(async () => {
  const res = await getTypesAPI();
  setTypes(res);
}, []);
return 
@ever-dev
ever-dev / ContentBuilder.vue
Last active October 8, 2020 18:07
Dynamic Page Builder for documentation.
<template>
<section :class="content.type" fluid class="content-container">
<!-- Content Header -->
<v-row>
<v-col v-if="content.showTitle" cols="12">
<p class="heading text-left text-xs-center d-flex align-center mb-5">
<v-icon
v-if="content.type === 'changelog'"
:class="{ open: collapse }"
@click="toggleCollapse()"
@ever-dev
ever-dev / asyncCallFromArray.js
Last active November 8, 2020 17:22
Call asynchronous functions in sequence
const arr = [...];
arr.reduce((p, cur) => {
return p.then(()=> {
return doSomething(cur);
});
}, Promise.resolve());
@ever-dev
ever-dev / error-handling-with-fetch.md
Created November 17, 2020 18:25 — forked from odewahn/error-handling-with-fetch.md
Processing errors with Fetch API

I really liked @tjvantoll article Handling Failed HTTP Responses With fetch(). The one thing I found annoying with it, though, is that response.statusText always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body.

Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the catch block to extract the message in the body:

fetch("/api/foo")
  .then( response => {
    if (!response.ok) { throw response }
    return response.json()  //we only get here if there is no error
 })