Skip to content

Instantly share code, notes, and snippets.

@norami
norami / merge.ts
Last active May 14, 2021 16:22
なるべく古いオブジェクトを残しつつ、新しいオブジェクトをコピーしたものを返す
const merge = (oldValue: any, newValue: any) => {
if (oldValue === newValue) {
return oldValue;
}
if (typeof oldValue !== "object" || typeof newValue !== "object") {
return newValue;
}
const createdObj: any = {};
let changed = false;
for (const key of Object.keys(oldValue)) {
@norami
norami / EventFCSample.ts
Last active March 30, 2021 04:46
Functional Component with minimum external store with event handling
import { EventEmitter } from "events";
import * as React from "react";
import { createContext, useContext, useState } from "react";
const useEmitter = <T extends EventEmitter>(emitter: T) => {
const [_updateCounter, setUpdateCounter] = useState(() => {
let counter = 0;
emitter.on("change", () => {
setUpdateCounter(++counter);
});