I have a higher order component that is used in a few different components. I'd like to be able to alter behaviour based on the component passed to the HOC:
const Field = Component => {
return class WrappedField extends React.Component {
const type = Component.name === 'EmailField' ? 'email' : 'text'
render() {
return <Component type={type} />
}
}
}
// Then used like so:
const TextField = props => <input />
export default Field(TextField)
This works in dev, but in production Component names are minified, so this fails. I have a few posts on the topic, like this one which suggests using React internals, or this one which suggests reading from the constructor, which does not seem to work.
The only workaround I have found is to do pass a second argument:
export default Field(TextField, 'TextField')
Which obviously does not get minified in production, but it seems crazy that the name cannot be dynamically read or saved.
Any suggestions for how to dynamically derive the component name in an HOC greatly appreciated.