Redux / ParseJS examples

#1

Does anybody have any examples of the JS SDK working nicely with redux? The only topics online seem to be from 3 years ago, when there was a maintained React library. In an attempt to get immutable objects, I tried the clone function, but this lost the objectID (naturally).

I’m currently just calling forceUpdate when I know that there is a change, but wondered if there is a better way?

Thanks for your guidance.

W

#2

I personally store Parse objects in the store, even if I’m not supposed to (because they’re mutable), but some people import/export JSON values.

#3

I did find the private API _toFullJSON() which would enable me to create immutable objects. How do you get around binding for a controlled input, when the state mutates?

#4

First of all, sorry for the late reply. I use a dirty trick as I store a version number next to my object, but the ease of use having the Parse.Object is so great that I forgive this hack to myself :slight_smile:

#5

It’s not pretty, but I needed something quick. In the end I used normalizr to create a schema of related JSON objects. Each object was immediately converted to its full JSON representation and passed around redux. When it came time to save the data I made a generic hydrator:

   const obj = Parse.Object.fromJSON(denormalize(entities, schema, getState()));
   const values = obj.toJSON();

   const bannedKeys = ['ACL', 'sessionToken'];
   Object.entries(values).forEach(([key, value]) => {
    if (bannedKeys.includes(key)) return;

    if (value && typeof value === 'object' && value.__type === 'Object') {
      value = Parse.Object.fromJSON(value);
    }

    const operation =
      !value || (typeof value === 'string' && value.length === 0)
        ? 'unset'
        : 'set';
    obj[operation](key, value);
  });

  return obj.save().then(obj => {
    const json = obj._toFullJSON();
    const data = normalize(json, schema);
    dispatch(addEntities(data.entities));
    return json;
  });

I realise this is pretty awful but it works for my use case. I did find a library called parse-lite which looked interesting, but outdated.