Created
October 31, 2018 02:32
-
-
Save na0x2c6/0fa14976866ea0367ed1e9934c00e9a9 to your computer and use it in GitHub Desktop.
Inheritance Inversion Sample 2
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
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