Last active
February 17, 2017 07:04
-
-
Save idiotWu/6dd144a7d70d9d7d98139ac7979a1834 to your computer and use it in GitHub Desktop.
React refs saver
This file contains 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
@storeRefs | |
class Demo extends React.Component { | |
doSomething() { | |
this.buttonRef.kill(); | |
} | |
render() { | |
return ( | |
<Button refs={this.storeRefAs('buttonRef')}>blabla</Button> | |
); | |
} | |
} |
This file contains 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
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