-
-
Save moimikey/9a91265d8b1a15acc42c8114d626aee8 to your computer and use it in GitHub Desktop.
filter out only `on` event props from react component
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
// ... | |
renderMenu() { | |
const { children, ...rest } = this.props || {}; | |
// only store props that startWith `on` in case the parent component | |
// wants to send event callbacks down, for subsequent use. | |
const eventProps = Object.keys(rest) | |
.filter(key => key.substr(0, 2) === 'on') // if startsWith 'on' (ie. onMouseOver) | |
.reduce((obj, key) => Object.assign(obj, { [key]: rest[key] }), {}); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could add an additional check to ensure that the 2nd index is an uppercase. mebe
key.substr(0, 2) === 'on' && key.substr(2,1) === key.substr(2,1).toUpperCase()