Part of a collection of Higher-Order Components for React, especially useful with Recompose.
Decouples e.preventDefault()
side effect from handlers like form submitting or clicking a link.
yarn add @hocs/prevent-handlers-default
preventHandlersDefault(
...handlers: Array<string>
): HigherOrderComponent
import React from 'react';
import { compose, withState, withHandlers } from 'recompose';
import preventHandlersDefault from '@hocs/prevent-handlers-default';
const submitAction = (value) => console.log(`Submit: ${value}`);
const Demo = ({ value, onChange, onSubmit }) => (
<form onSubmit={onSubmit}>
<input value={value} onChange={onChange}/>
<button>Submit</button>
</form>
);
export default compose(
withState('value', 'setValue', ''),
withHandlers({
onChange: ({ setValue }) => (e) => setValue(e.target.value),
onSubmit: ({ value }) => () => submitAction(value)
}),
preventHandlersDefault('onSubmit')
)(Demo);