Skip to content

Instantly share code, notes, and snippets.

@idiotWu
Last active February 17, 2017 07:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idiotWu/6dd144a7d70d9d7d98139ac7979a1834 to your computer and use it in GitHub Desktop.
Save idiotWu/6dd144a7d70d9d7d98139ac7979a1834 to your computer and use it in GitHub Desktop.
React refs saver
@storeRefs
class Demo extends React.Component {
doSomething() {
this.buttonRef.kill();
}
render() {
return (
<Button refs={this.storeRefAs('buttonRef')}>blabla</Button>
);
}
}
export const storeRefs = (Class) => {
Object.defineProperty(Class.prototype, 'storeRefAs', {
enumerable: false,
configurable: true,
writable: true,
value: function storeRefAs(name) {
const savers = this._savers = this._savers || {};
if (!savers[name]) {
savers[name] = ref => {
this[name] = ref;
};
}
return savers[name];
},
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment