Skip to content

Instantly share code, notes, and snippets.

View yongdamsh's full-sized avatar

Lee Sanghyeon yongdamsh

  • Incheon, Korea
View GitHub Profile
@yongdamsh
yongdamsh / useWindowSize.js
Last active August 30, 2020 13:33
React custom hook to subscribe resize event
import { useReducer } from 'react';
import debounce from 'lodash/debounce';
function windowSizeReducer(state, action) {
if (action.type === 'resize') {
return action.width;
}
return state;
}
@yongdamsh
yongdamsh / useElemenetVisibility.js
Created June 7, 2019 08:28
React custom hook based on intersectionObserver
import 'intersection-observer';
import { useCallback, useState, useEffect, useRef } from 'react';
const defaultEntry = {
isIntersecting: false,
intersectionRatio: 0
};
const defaultOptions = {};
function useElementVisibility(options = defaultOptions) {
import React, { useEffect, useRef } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
// Custom hook
function useLazyImageObserver(target) {
useEffect(() => {
if (!target.current) {
return;