Skip to content

Instantly share code, notes, and snippets.

@rbalicki2
Last active June 13, 2019 17:23
Show Gist options
  • Select an option

  • Save rbalicki2/b9b631c831c5870ffc26143fbeed1275 to your computer and use it in GitHub Desktop.

Select an option

Save rbalicki2/b9b631c831c5870ffc26143fbeed1275 to your computer and use it in GitHub Desktop.

Revisions

  1. rbalicki2 revised this gist Jun 13, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Form.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,6 @@ export default class Model {
    */
    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;
  2. rbalicki2 created this gist Jun 13, 2019.
    28 changes: 28 additions & 0 deletions Form.js
    Original 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];
    };
    }