Skip to content

Instantly share code, notes, and snippets.

View VinayakBagaria's full-sized avatar
💭
So much to experiment on..

Vinayak Bagaria VinayakBagaria

💭
So much to experiment on..
View GitHub Profile
@VinayakBagaria
VinayakBagaria / deep.js
Created August 12, 2019 07:39
Deep equals on 2 variables where a is compared with b
function deepEquals(a, b) {
if(a === b) {
return true;
}
if(a && b && typeof a === 'object' && typeof b === 'object' && Object.keys(a).length === Object.keys(b).length && Array.isArray(a) === Array.isArray(b)) {
for(let key in a) {
if(key in b) {
if(!deepEquals(a[key], b[key]) {
return false;
@VinayakBagaria
VinayakBagaria / Navbar.js
Created June 10, 2019 12:50
Navbar change as per auth is changed
function Navbar() {
const user = useAuth()
return user ? <UserNavActions user={user} /> : <GuestNavActions />
}
function useAuth() {
const [user, setUser] = useState()
useEffect(() => {
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
@VinayakBagaria
VinayakBagaria / isInViewport.js
Last active March 15, 2019 10:02
Check if an element is in viewport
// pass an ElementType and get boolean
export const isInViewport = el => {
const rect = el.getBoundingClientRect();
return (
rect.right >= 0 &&
rect.bottom >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
);
}
@VinayakBagaria
VinayakBagaria / statecomparer.js
Created February 9, 2019 13:00
Hooks compare current state and prev state
function Component() {
const [count, setCount] = useState(0);
const prevCount = useRef(null);
useEffect(() => {
// only for the first render, where 'decreased' would have been shown.
if(prevCount.current === null) {
prevCount.current = count;
return;
@VinayakBagaria
VinayakBagaria / forceupdate.js
Last active February 9, 2019 12:49
useState bails out if next value is same as previous one, this is how we can forceUpdate the Component
import React, { useReducer } from 'react';
function Component() {
const [ignored, forceUpdate] = useReducer(x => x + 1, 0);
function handleClick() {
forceUpdate();
}
return (
@VinayakBagaria
VinayakBagaria / app.js
Created February 9, 2019 12:38
React Hooks don't run on 1st render
import React, { useState, useEffect, useRef } from 'react';
function Abc() {
const [abc, setAbc] = useState(1);
const isFirstRun = useRef(true);
useEffect (() => {
if (isFirstRun.current) {
isFirstRun.current = false;
return;
@VinayakBagaria
VinayakBagaria / serviceWorker.js
Created December 20, 2018 07:43
Service worker CRA
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
@VinayakBagaria
VinayakBagaria / redux.js
Created December 7, 2018 20:59
Minified version of how Redux works
function createStore(reducer) {
var state;
var listeners = []
function getState() {
return state
}
function subscribe(listener) {
listeners.push(listener)
@VinayakBagaria
VinayakBagaria / dynamic.js
Created December 7, 2018 19:56
Conditional promise import of polyfills. You can get the functionality + support + bundle size. Simple difference illustrated.
import app from './app.js'
const polyfills = []
if (!window.fetch) {
polyfills.push(import(/* webpackChunkName: "polyfill-fetch" */ 'whatwg-fetch'))
}
Promise.all(polyfills)
.then(app)
.catch((error) => {
console.error('Failed fetching polyfills', error)
})
@VinayakBagaria
VinayakBagaria / GalleryBox.js
Last active December 7, 2018 19:52
Image Observer React Gallery Box
// @flow
import React, { PureComponent } from 'react';
import GalleryBoxWithIntersection from './GalleryBoxWithIntersection';
import GalleryBoxWithoutIntersection from './GalleryBoxWithoutIntersection';
type Image = {
url: string,
height: number,
width: number,