Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example in documentation on how to optimize with Store watch handlers #8

Open
nicolashery opened this issue Jul 23, 2014 · 2 comments

Comments

@nicolashery
Copy link

The handler passed to Store.addWatch is a function(keys, oldState, newState). I think that's great, but at first it wasn't obvious how I would optimize React's re-render by using the keys. Now it's clearer, but there is still the question of when keys is an array (I'm guessing a deep compare, with Lo-Dash maybe? or a JSON.stringify with equality check?). Could it be worth adding an example to the README for that?

@jmreidy
Copy link
Owner

jmreidy commented Jul 28, 2014

The following comment already exists:

Note the keys argument of the watcher handler. It's a reference to string or array of keys passed into the set function. Using keys, you can easily figure out whether your watcher handler should care about the updated state (or not).

How should this be elaborated on?

@nicolashery
Copy link
Author

Good question. Maybe an example? But that might not be necessary...

Initially I was wondering what to do when keys is an array. I thought of something like:

function arrayEquals(arr1, arr2) {
  return mori.equals(mori.js_to_clj(arr1), mori.js_to_clj(arr2));
}

Then:

// PublicProfile.react.js
handleUserStoreChange: function(keys, oldState, newState) {
  if (arrayEquals(keys, ['user', 'publicProfile']) {
    return;
  }
  this.setState({profile: UserStore.get(['user', 'publicProfile']});
}

However, I realized that I'm mostly watching for changes on stores' top-level objects, so keys is almost always a string.

On a related subject, I wondered if I should optimize using the components' shouldComponentUpdate method, or the handleStoreChange methods, or both. I ended up using shouldComponentUpdate in my controllers (React component with state as mori data structures, so easy to check for change in state), but the question is still up in the air :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants