Skip to content

Instantly share code, notes, and snippets.

@moimikey
Created January 6, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moimikey/9a91265d8b1a15acc42c8114d626aee8 to your computer and use it in GitHub Desktop.
Save moimikey/9a91265d8b1a15acc42c8114d626aee8 to your computer and use it in GitHub Desktop.
filter out only `on` event props from react component
// ...
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] }), {});
// ...
}
@moimikey
Copy link
Author

moimikey commented Jan 6, 2018

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment