Skip to content

Instantly share code, notes, and snippets.

@McCulloughRT
Created October 11, 2017 01:10
Show Gist options
  • Save McCulloughRT/004967b12f69de5f20df4cb7b5e97ada to your computer and use it in GitHub Desktop.
Save McCulloughRT/004967b12f69de5f20df4cb7b5e97ada to your computer and use it in GitHub Desktop.
componentWillReceiveProps(nextProps) {
// Prevent the diff'ing if function is
// triggered before a style is set
if(this.props.mapStyle === null) return;
const map = this.map;
// Remember where we are in the lifecycle here,
// even though we already update the Redux state,
// this component's current properties still
// reflect the old state:
const oldStyle = this.props.mapStyle;
const newStyle = nextProps.mapStyle;
// Do a deep equality check first to make sure
// an update needs to happen. Avoid unnessecary
// converting and diff'ing:
if(!Immutable.is(oldStyle, newStyle)) {
const changes = diffStyles(before.toJS(), after.toJS());
// 'changes' will be an array of:
// {command: '...', args: ['...', ...]}
// objects that can be applied to the map:
changes.forEach(function(change) {
map[change.command].apply(map, change.args);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment