Last active
June 13, 2019 17:23
-
-
Save rbalicki2/b9b631c831c5870ffc26143fbeed1275 to your computer and use it in GitHub Desktop.
Revisions
-
rbalicki2 revised this gist
Jun 13, 2019 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,6 @@ export default class Model { */ static cache = {}; static getFromCache(obj) { const stringified = JSON.stringify(obj); if (!this.cache[stringified]) { const Constructor = this; -
rbalicki2 created this gist
Jun 13, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ import React from 'react'; import { Form } from 'react-final-form'; export default ({ render, transform, ...rest })=> <Form {...rest} render={({ values, ...renderPropsRest }) => render({ values: transform(values), ...renderPropsRest })} />; // the function passed to transform is x => MyModelSubclass.getFromCache(x) export default class Model { /** * What are we doing here? * We are stringifying an object, then storing the Model * version of that in a cache. That way, we can avoid re-creating * it later when we have the same object. */ static cache = {}; static getFromCache(obj) { // N.B. this originalData omission is a smell. const stringified = JSON.stringify(obj); if (!this.cache[stringified]) { const Constructor = this; this.cache[stringified] = new Constructor(obj); } return this.cache[stringified]; }; }