Skip to content

Instantly share code, notes, and snippets.

View utsavdotpro's full-sized avatar
👨‍💻
Building

Utsav Barnwal utsavdotpro

👨‍💻
Building
View GitHub Profile
@utsavdotpro
utsavdotpro / ConditionalWrapper.tsx
Last active May 15, 2025 08:54
ConditionalWrapper.tsx
type Props = {
toWrap: boolean;
wrapper: (children: any) => any;
};
export const ConditionalWrapper: React.FC<Props> = ({ toWrap, wrapper, children }) => {
return toWrap ? wrapper(children) : children;
};
@utsavdotpro
utsavdotpro / useQueryParams.ts
Created September 8, 2023 09:37
Wrapper on top of react-router's useLocation hook to return query params
import { useLocation } from 'react-router';
const useQueryParams = (...keys: string[]) => {
const location = useLocation();
const params = new URLSearchParams(location.search);
const values = keys.map((key) => params.get(key) ?? '');
return values;
};
@utsavdotpro
utsavdotpro / useForceRemount.ts
Last active April 15, 2023 07:45
Use custom key to remount a component on programmatically
import { useCallback, useState } from "react";
const useForceRemount = () => {
const [key, setKey] = useState(Math.random());
const forceRemount = useCallback(() => setKey((key) => key + 1), []);
return { mountKey: key, forceRemount };
};
export default useForceRemount;
@utsavdotpro
utsavdotpro / commit-comment.yml
Created September 22, 2022 07:37
Add commit comment on a GitLab MR from pipeline job (CI)
# How to use?
# commit_comment "YOUR_MESSAGE"
.commit-comment:
script:
- export GITLAB_TOKEN="PROJECT_ACCESS_TOKEN"
- |
commit_comment() {
curl --location --request POST "https://gitlab.agodadev.io/api/v4/projects/$CI_MERGE_REQUEST_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --header "Content-Type: application/json" --data-raw "{ \"body\": \"$1\" }"
}
import { useEffect, useRef, useState } from 'react';
export function useDelayedProps<T>(props: T, duration: number): [boolean, T] {
const delayedProps = useRef(props);
const [toRerender, setToRerender] = useState(false);
useEffect(() => {
setToRerender(true);
const timeout = setTimeout(() => {
import { useLayoutEffect, useState } from 'react';
import isEqual from 'lodash/isEqual';
export function useDelayedProps<T>(props: T, duration: number, idProp?: keyof T): T {
const [delayedProps, setDelayedProps] = useState(props);
useLayoutEffect(() => {
let timeout: NodeJS.Timeout;
const isPropEqual = idProp
? props[idProp] === delayedProps[idProp]
@utsavdotpro
utsavdotpro / .zshrc
Last active September 12, 2023 07:20
Alias for commonly used Git, Yarn, NPM, Nest commands
# Git
# --------------------------------
alias g="git"
alias gi="git init"
alias gc="git commit -m"
alias gcnv="git commit --no-verify -m"
alias gcae="git commit --allow-empty -m"
@utsavdotpro
utsavdotpro / useRerenderAnimation.tsx
Created January 19, 2022 09:44
useRerenderAnimation hook takes your props and two animations that you wish show between rerenders and toggles them every time your props change.
import { useEffect, useState } from 'react';
function useRerenderAnimation<PropType>(
props: PropType,
initialAnimation: string,
renderAnimation: string,
renderDelay: number // in milliseconds, ideally should be equal to the duration of renderAnimation
) {
const [state, setState] = useState<PropType>();
const [rendered, setRendered] = useState(true);