Skip to content

Instantly share code, notes, and snippets.

@Retsam
Retsam / removeUnused.js
Created February 13, 2024 22:31
ts-morph script to migrate to react-jsx
const { Project } = require("ts-morph");
/**
* Loops through all TSX files and removes unused imports,
* which will cleanup the `import React from "react"` if it's no longer needed
*/
const project = new Project({
tsConfigFilePath: "tsconfig.json",
});
@Retsam
Retsam / example.tsx
Last active September 19, 2022 18:01
Minimal data fetching LoadState
type LoadState<T> =
| { state: "loading" }
| { state: "loaded", data: T }
// Not all usecases will have an error state, in which case it's fine to omit it here.
| { state: "error", err: string };
type ExamplePayload = string;
const ExampleComponent = () => {
const [ loadState, setLoadState ] = useState<LoadState<ExamplePayload>>({ state: "loading" });
useEffect(() => {
@Retsam
Retsam / exampleState.ts
Last active January 31, 2021 18:11
Redux-like useReducer pattern
import { Dispatch, useReducer, useEffect } from "react";
import { produce } from "immer";
//=======
// State
//=======
export type ExampleState = {
name: string;
level: number;
saved: false;
@Retsam
Retsam / a.js
Created September 13, 2017 01:06
Webpack CommonsChunkPlugin issue
import c from "./c";
export default "a" + c;
@Retsam
Retsam / app.js
Last active February 8, 2016 20:03
App Versioning Structure
import v1 from "./v1"
import v2 from "./v2"
import express from "express";
var app = express();
app.use("/v1", v1);
app.use("/v2", v2);
@Retsam
Retsam / BabelBug.js
Last active August 29, 2015 14:25
BabelJS Bug
//Expected output: A B C (see http://www.es6fiddle.net/ice1qchm/)
//Actual output from Babel: A C
//ES5 legacy code
function A () {
console.log("A");
}
function B () {