Skip to content

Instantly share code, notes, and snippets.

@AndrejGajdos
Last active January 31, 2016 17:53
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 AndrejGajdos/d4d9c802bf1b37165fd9 to your computer and use it in GitHub Desktop.
Save AndrejGajdos/d4d9c802bf1b37165fd9 to your computer and use it in GitHub Desktop.
Data props diffing in a React component and re-initialize a cell of jQuery object without re-initializing jQuery grid object in the componentDidUpdate method (and without re-rendering the whole React component). Code snippet in blog post http://andrejgajdos.com/how-to-avoid-refactoring-in-your-first-react-application/
let diff = require('immutablediff');
shouldComponentUpdate(nextProps, nextState) {
let propsDiff = diff(this.props.data, nextProps.data);
if (propsDiff.size === 1) {
let operation = propsDiff.get(0).get("op");
let replacedObj = propsDiff.get(0).get("value");
switch (operation) {
case "replace":
if (Immutable.Map.isMap(replacedObj)) {
let editedObj = {};
let xCoordinate;
let yCoordinate;
for (let entry of replacedObj.entries()) {
if (entry[0] === 'x-coordinate'){
xCoordinate = entry[1];
}
if (entry[0] === 'y-coordinate'){
yCoordinate = entry[1];
}
editedObj[entry[0]] = entry[1];
}
$(ReactDOM.findDOMNode(this.refs.dataGrid))
.updateCellOnCoordinate(xCoordinate, yCoordinate, editedObj);
return false;
}
break;
case "remove":
...
break;
case "add":
...
break;
default:
return true;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment