Skip to content

Instantly share code, notes, and snippets.

@rbalicki2
Last active June 13, 2019 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbalicki2/b9b631c831c5870ffc26143fbeed1275 to your computer and use it in GitHub Desktop.
Save rbalicki2/b9b631c831c5870ffc26143fbeed1275 to your computer and use it in GitHub Desktop.
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