Skip to content

Instantly share code, notes, and snippets.

@wiredprairie
wiredprairie / decorator1.tsx
Created April 21, 2018 02:25
React and TypeScript decorator
import * as React from "react";
export function attachModel<T>(Model: { new (): T }) {
return function<Props, State>(Comp: React.ComponentClass<Props>) {
const ComponentModel = (Comp as any) as React.ComponentClass;
return class extends React.Component<Props, State> {
static displayName = `AttachedModel${ComponentModel.displayName ||
ComponentModel.name}`;
render() {
return <ComponentModel {...this.props} />;
@wiredprairie
wiredprairie / zustand-initStore-experiement.ts
Created May 5, 2023 18:58
Experiment with Zustand initializer
function initStore<StoreTypes, Store extends UseBoundStore<StoreApi<unknown>>>(
store: Store,
compare?: (a: StoreTypes, b: StoreTypes) => boolean
) {
return (newValue: StoreTypes, defaultValue: StoreTypes) => {
if (compare && compare(newValue, store.getState() as unknown as StoreTypes)) {
return
}
store.setState(newValue as any)
}