Skip to content

Instantly share code, notes, and snippets.

@na0x2c6
Created October 31, 2018 02:32
Show Gist options
  • Save na0x2c6/0fa14976866ea0367ed1e9934c00e9a9 to your computer and use it in GitHub Desktop.
Save na0x2c6/0fa14976866ea0367ed1e9934c00e9a9 to your computer and use it in GitHub Desktop.
Inheritance Inversion Sample 2
function addPrefixToName(WrappedComponent, prefix) {
return class extends WrappedComponent {
render() {
const wrappedTree = super.render()
let newProps = {}
if (wrappedTree && wrappedTree.type === 'input') {
newProps = {name: `${prefix}-${wrappedTree.props.name}`}
}
const props = { ...wrappedTree.props, ...newProps } // Object.assign({}, wrappedTree.props, newProps) と同様
const newTree = React.cloneElement(wrappedTree, props, wrappedTree.props.children)
return newTree
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment