Skip to content

Instantly share code, notes, and snippets.

Last active Jun 13, 2019
What would you like to do?
import React from 'react';
import { Form } from 'react-final-form';
export default ({ render, transform, })=> <Form
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