Last active
August 29, 2015 14:24
-
-
Save dmitry-vsl/f2f68697c63c09a722b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Миксин для React компонентов, который переопределяет shouldComponentUpdate на | |
основе глубокого сравнения старого и нового значения. | |
В миксин надо передать функцию, которая возвращает сравниваемый объект. | |
Пример использования: | |
``` | |
var MyStore = require('myStore'); | |
var FooComp = React.createClass({ | |
mixins: [deepEqualsRender(() => MyStore.foo)] | |
}); | |
``` | |
*/ | |
define(function(){ | |
return function(selector){ | |
function valueToStr(props){ | |
return JSON.stringify(selector(props)); | |
} | |
return { | |
componentDidMount(){ | |
this.__prevDataStr = valueToStr(this.props); | |
}, | |
shouldComponentUpdate(nextProps){ | |
var nextDataStr = valueToStr(nextProps); | |
if(nextDataStr != this.__prevDataStr){ | |
this.__prevDataStr = nextDataStr; | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
}; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment