Last active
June 13, 2019 17:23
-
-
Save rbalicki2/b9b631c831c5870ffc26143fbeed1275 to your computer and use it in GitHub Desktop.
This file contains 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 characters
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) { | |
const stringified = JSON.stringify(obj); | |
if (!this.cache[stringified]) { | |
const Constructor = this; | |
this.cache[stringified] = new Constructor(obj); | |
} | |
return this.cache[stringified]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment