Posts

Showing posts from April, 2018

add editor state vars with typescript an recompose

add editor state vars with typescript an recompose If you need to add editor state to any class you can use recompose. Here’s the basics: /** What the output of [addEditorState] will receive as input. */ export interface EditorProps { isDirty : boolean isEditing : boolean setDirty : ( v : boolean ) => void setEditing : ( v : boolean ) => void resetDirty : ( ) => void resetEditing : ( ) => void } and the recompose part: /** * HOC to add isDirty and isEditing state management plus a few functions. * Input component goes from from `P extends EditorProps` => `P omit EditorProps`. */ export function addEditorState < P extends EditorProps > ( component : React . ComponentType < P > ) { return compose < P , Omit < P , EditorProps >> ( withState ( "isEditing" , "setEditing" , false ) , withState ( "isDirty" , "setDirty"