Skip to content

Instantly share code, notes, and snippets.

@gnbaron
gnbaron / getReduxStore.js
Last active October 10, 2023 20:43
Get Redux store in the browser console
window.getReduxStores = () => {
const stores = []
const traverse = (element) => {
const store =
element?.memoizedState?.element?.props?.store ||
element?.pendingProps?.store ||
element?.stateNode?.store
if (store) {
@gnbaron
gnbaron / index.tsx
Created December 16, 2021 17:39
Responsive
import React, { useState, useEffect, ReactNode } from "react"
export function useMediaQuery(query: string) {
const [matches, setMatches] = useState(false)
useEffect(() => {
const media = window.matchMedia(query)
if (media.matches !== matches) {
setMatches(media.matches)
}
@gnbaron
gnbaron / flushPromises.ts
Created July 2, 2021 00:02
Flush promises
/**
* A promise that resolves on next tick.
* Beneficial for waiting for immediately resolved promises without
* lengthy promise chains.
*/
export const flushPromises = (): Promise<void> =>
new Promise(resolve => setImmediate(resolve))
@gnbaron
gnbaron / index.js
Created May 18, 2021 16:52
Check attributes added to window object.
function getAttrs() {
const iframe = document.createElement('iframe')
document.body.appendChild(iframe)
return Object.entries(window)
.filter(([k]) => !(k in iframe.contentWindow))
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})
}
@gnbaron
gnbaron / index.js
Created March 22, 2021 21:20
format number with commas in js
export function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Remove default padding */
ul[class],
ol[class] {
@gnbaron
gnbaron / README.md
Last active January 31, 2020 12:05
Convert flow typed JS files to Typescript using @khanacademy/flow-to-ts.

Usage: npx https://gist.github.com/gnbaron/48d417e54fb9527a24422546b3ef763c src/folder