Skip to content

Instantly share code, notes, and snippets.

View JayMGurav's full-sized avatar
🧑‍💻
☕😴🏃‍♂️💪🛣🤝✨🚀

Jay Gurav JayMGurav

🧑‍💻
☕😴🏃‍♂️💪🛣🤝✨🚀
View GitHub Profile
@JayMGurav
JayMGurav / NoSSR.jsx
Created June 17, 2023 23:10
NextJs NoSSR component
import dynamic from 'next/dynamic'
import React from 'react'
const NoSsr = props => (
<React.Fragment>{props.children}</React.Fragment>
)
export default dynamic(() => Promise.resolve(NoSsr), {
ssr: false
})
@JayMGurav
JayMGurav / getAllFilesRecursively.js
Last active February 9, 2023 04:12
Recursively traverse through the directory and list out all the files asynchronously
"use strict";
const { readdir } = require("fs");
const { promisify } = require("util");
const readdirP = promisify(readdir);
async function getAllFilesRecursively(folder) {
return await (
@JayMGurav
JayMGurav / serializeRandomTimer.js
Last active July 15, 2021 02:47
serialize random timers to print numbers from 0-10 in order
function randomTime() {
const min = 0, max = 6;
return (Math.floor(Math.random() * (max - min + 1)) + min) * 1000;;
}
Array.from({length:10}, (_, k) => k+1).reduce(async (prev, curr) => {
await prev;
return new Promise((resolve) => {
setTimeout(resolve,randomTime(), console.log(curr))
});
@JayMGurav
JayMGurav / useViewportSize.ts
Last active July 7, 2021 12:33
useViewportSize hook to get the view port size
import { useEffect, useState } from 'react';
import debounce from 'lodash.debounce';
interface ViewportSize {
width: number;
height: number;
}
function useViewportSize() {
const [viewportSize, setViewportSize] = useState<ViewportSize>({
@JayMGurav
JayMGurav / ThemeContext.tsx
Created June 9, 2021 08:33
Dark Mode using Next.js
import {
COLORS,
COLOR_MODE_KEY,
INITIAL_COLOR_MODE_CSS_PROPERTY,
} from '@/styles/themeConfig';
import { createContext, useEffect, useMemo, useState } from 'react';
import { ChildrenOnlyProps, ColorMode } from '@/types/index';
export const ThemeContext = createContext<{
@JayMGurav
JayMGurav / useLocalStorage.js
Created May 2, 2021 04:08
A custom hook to use localStorage
import { useState, useEffect } from "react";
const useLocalStorage = (initialState, key) => {
const [value, setValue] = useState(() => {
const keyValue = localStorage.getItem(key);
if(keyValue) return JSON.parse(keyValue)[value];
return initialState;
});
useEffect(() => {
@JayMGurav
JayMGurav / scrollToTop.js
Last active May 2, 2021 04:00
A custom scroll-to-top hook
import { useEffect, useState } from "react";
export default function scrollToTop(){
const [isBtnVisible, setIsBtnVisible] = useState(false);
const [pageYOffset, setPageYOffset] = useState(0);
useEffect(()=>{
const checkVisiblity = () => {
setPageYOffset(window.pageYOffset);
if(pageYOffset > window.innerHeight){
@JayMGurav
JayMGurav / ohmyposh.mytheme.json
Created March 10, 2021 05:11
My ohmyposh theme for powershell/windows terminal
{
"final_space": false,
"osc99": false,
"console_title": false,
"console_title_style": "",
"console_title_template": "",
"blocks": [
{
"type": "prompt",
"alignment": "left",
@JayMGurav
JayMGurav / sieveOfEratosthenes.js
Created February 24, 2021 02:47
The sieve of Eratosthenes is an ancient algorithm and is one of the most efficient ways to find all primes up to any given limit.
function seive(n){
const arr = Array.from({length: n}, (_,i) => i > 1 ? i : false),
limit = Math.sqrt(n);
for(let i = 0; i < limit; i++){
let j = i*i;
while (j <= n) {
arr[j] = false;
j = j+i;
}
@JayMGurav
JayMGurav / mergeRefs.js
Created February 15, 2021 10:58
Merge React ref's
import React from 'react';
export default function mergeRefs(...refs){
const filteredRefs = refs.filter(Boolean);
return React.useMemo(() => {
if(!filteredRefs.length) return null;
return (node) => {
filteredRefs.forEach(ref => {
if(ref) assignRef(ref, node);
});