Skip to content

Instantly share code, notes, and snippets.

@madflanderz
madflanderz / useDebounce.ts
Created February 20, 2019 15:05
useDebounce hook
import { useState, useEffect } from 'react'
const useDebounce = (fn: () => any, ms: number = 0, args: any[] = []) => {
const [timeout, setTimeoutVar] = useState<any>(null)
const [isStart, setStart] = useState(true)
useEffect(() => {
// don't start interval when executed first time
if (isStart) {
setStart(false)
@madflanderz
madflanderz / useDebouncedState.ts
Last active February 24, 2021 11:31
useDebouncedState hook
import { useState, Dispatch, SetStateAction, useEffect } from "react"
/**
* useDebouncedState can be used to debounce value updates for given delay.
*
* Example Usecase: You have a search field and want to load sugestions.
* Api should only be called when last user interaction is minimum 300ms ago.
*
* Usage: debouncedValue is updated after 500 ms
* const [value, debouncedValue, setValue] = useDebouncedState("", 500)
*/
@madflanderz
madflanderz / useValueChanged.ts
Created February 20, 2019 15:06
useValueChanged hook
import { useRef } from 'react'
function useValueChanged<T>(callback: (value: T) => void, value: T) {
// save initial value on first call
const ref = useRef(value)
// execute callback when value has changed
if (ref.current !== value) {
ref.current = value
callback(value)
@madflanderz
madflanderz / ComponentMaterialUI.tsx
Last active March 7, 2019 09:31
React Component Material UI
import * as React from 'react'
import { Theme } from '@material-ui/core/styles'
import { makeStyles } from '@material-ui/styles';
const useStyles = makeStyles(({ palette }: Theme) => ({
root: {
// backgroundColor: palette.primary,
},
}),
{ name: 'MyComponent' }
@madflanderz
madflanderz / useSmartQuery.ts
Created February 27, 2019 17:07
useSmartQuery - useQuery hook with default fetchPolicy
import { useQuery, QueryHookOptions } from 'react-apollo-hooks'
function useSmartQuery<S, T>(query: any, options?: QueryHookOptions<T>) {
return useQuery<S>(query, {
fetchPolicy: 'cache-and-network',
...options,
})
}
export default useSmartQuery
@madflanderz
madflanderz / makeVSCodeSnippet.js
Last active February 28, 2019 12:31
Crome Snippet to create a VSCode Snippet
window.makeSnippet = (code, prefix="ccc", componentName="MyComnponent") => {
console.log('makeSnippet()', code)
code = code.replace(new RegExp(componentName, 'g'), '$TM_FILENAME_BASE')
console.log('componentName: ', componentName)
const lines = code.split('\n')
console.log(lines)
@madflanderz
madflanderz / Component.ts
Created March 7, 2019 09:49
TypeScript React Component
import React from 'react'
interface Props {
title: string
}
const StatusIcon = ({ title }: Props) => (
<div>
</div>
@madflanderz
madflanderz / useFormik.ts
Created March 8, 2019 11:32
useFormik hook
import React from 'react'
import { FormikValues, FormikErrors } from 'formik'
/** @private is the given object an Object? */
export const isObject = obj => obj !== null && typeof obj === 'object'
export function setNestedObjectValues(object, value, visited = new WeakMap(), response = {}) {
for (let k of Object.keys(object)) {
const val = object[k]
if (isObject(val)) {
// create a bookmark and use this code as the URL, you can now toggle the css on/off
// thanks+credit: https://dev.to/gajus/my-favorite-css-hack-32g3
javascript: (function() {
var elements = document.body.getElementsByTagName('*');
var items = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].innerHTML.indexOf('* { background:#000!important;color:#0f0!important;outline:solid #f00 1px!important; background-color: rgba(255,0,0,.2) !important; }') != -1) {
items.push(elements[i]);
}
}
@madflanderz
madflanderz / extractTypeFromArray.ts
Last active October 25, 2019 13:11
Extract Type from Array
const food = ['apple', 'banana', 'cookie'] as const
// 'apple', 'banana', 'cookie'
type Food = typeof food[number]