Skip to content

Instantly share code, notes, and snippets.

@mattheu
Created May 7, 2019 22:46
Show Gist options
  • Save mattheu/d4756e69a5304c3f609e53001ea04c6c to your computer and use it in GitHub Desktop.
Save mattheu/d4756e69a5304c3f609e53001ea04c6c to your computer and use it in GitHub Desktop.
import withUniqueId from './with-unique-id';
function FormField({
id,
label,
value,
onChange,
}) {
return (
<div>
<label for={ `field-${ id }` }>{ label }</label>
<input id={ `field-${ id }` } type="text" value={ value } onChange={ onChange }/>
</div>
);
}
export withUniqueId( FormField );
export function withUniqueId( WrappedComponent ) {
return class extends React.Component {
constructor( props ) {
super( props );
this.id = _.uniqueId();
}
render() {
return (
<WrappedComponent
{ ...this.props }
id={ this.id }
/>
);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment