Is there a way for PropTypes from a component inside of a Higher Order Component to point back to where they were created?
This is a small sample but if there was multiple EnhancedButtons
throughout an application in separate files this would be very hard to debug.
Since the Higher Order Component is ideally made for reusability we may never know the location of the component that is missing the handleClick method. The render method of _EnhancedButton
is a variable for any Component
that we want enhanced.
Is there any way to make the PropTypes more obvious where they are being created such as FinalButton
which is inserted and is an instance of _EnhancedButton
and is missing the prop handleClick?
https://jsfiddle.net/kriscoulson/sh2b8vys/3/
var Button = (props) => (
<button onClick={ () => props.handleClick() }>
Submit
</button>
);
Button.propTypes = {
handleClick: React.PropTypes.func.isRequired
}
const EnhanceButton = Component => class _EnhancedButton extends React.Component {
render () {
return (<Component { ...this.props }>{this.props.children}</Component>);
}
}
const FinalButton = EnhanceButton(Button);
ReactDOM.render(
<FinalButton />,
document.getElementById('container')
);
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive an error at the very end of our app by the console which might not be easy to deal with.
In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by React.memo or React.forwardRef . PropTypes exports a range of validators that can be used to make sure the data you receive is valid.
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature. Concretely, a higher-order component is a function that takes a component and returns a new component.
Higher-Order Components are not part of the React API. They are the pattern that emerges from React's compositional nature. The component transforms props into UI, and a higher-order component converts a component into another component. The examples of HOCs are Redux's connect and Relay's createContainer.
The name FinalButton
in your example won't be known to react since that is just your local variable name, but we change the name of the resulting component to whatever you want. Here I use "Final" in front of whatever the original name was.
Also, we can copy / merge the prop types over to the new element.
function EnhanceButton(Component) {
class _EnhancedButton extends React.Component {
static displayName = 'Final' + (Component.displayName || Component.name || 'Component');
render() {
return (
<Component { ...this.props }>{this.props.children}</Component>
);
}
}
_EnhancedButton.propTypes = Component.propTypes;
return _EnhancedButton;
}
This gives: Warning: Failed propType: Required prop handleClick
was not specified in Button
. Check the render method of FinalButton
.
Fiddle: https://jsfiddle.net/luggage66/qawhfLqb/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With