Skip to content

Instantly share code, notes, and snippets.

View nirajrajgor's full-sized avatar

Niraj M. Rajgor nirajrajgor

View GitHub Profile
@nirajrajgor
nirajrajgor / react.memo.js
Created January 19, 2021 09:37
React.memo native implementation (Polyfill)(Asked in Interview)
const shallowCompare = (prev, next) => {
for (let key in next) {
if (next[key] !== prev[key]) return false;
}
return true;
};
export function memo(Component, areEqual = shallowCompare) {
let prevProps = {};
let prevResult = JSX.Element | undefined;
return (nextProps) => {
@nirajrajgor
nirajrajgor / dfs-post-order.js
Created September 21, 2020 11:33
Depth First Search with Post Order Traversal in JavaScript
class Node {
constructor(data) {
this.data = data;
this.children = [];
}
add(data) {
this.children.push(new Node(data));
}
remove(data) {
this.children = this.children.filter(child => child.data !== data);
@nirajrajgor
nirajrajgor / dfs-pre-order.js
Last active September 21, 2020 11:20
Depth First Search with Pre Order in Javascript
class Node {
constructor(data) {
this.data = data;
this.children = [];
}
add(data) {
this.children.push(new Node(data));
}
remove(data) {
this.children = this.children.filter(child => child.data !== data);
@nirajrajgor
nirajrajgor / bfs.js
Last active September 21, 2020 10:50
Breadth First Search in Javascript
class Node {
constructor(data) {
this.data = data;
this.children = [];
}
add(data) {
this.children.push(new Node(data));
}
remove(data) {
this.children = this.children.filter((child) => child.data !== data);
@nirajrajgor
nirajrajgor / tree.js
Created September 20, 2020 17:33
Tree & its Node using ES6 Classes in javascript.
class Node {
constructor(data) {
this.data = data;
this.children = [];
}
add(data) {
this.children.push(new Node(data));
}
remove(data) {
this.children = this.children.filter(child => child.data !== data);
@nirajrajgor
nirajrajgor / ImageLoad.js
Created July 19, 2020 15:14
Progressive load image component in react with hooks
import React, { useState, useEffect } from 'react';
const ImageLoad = React.memo(({ src, placeholder, alt = "" }) => {
const [loading, setLoading] = useState(true);
const [currentSrc, updateSrc] = useState(placeholder);
useEffect(() => {
// start loading original image
const imageToLoad = new Image();
imageToLoad.src = src;