@hocs/prevent-handlers-default

0.4.0 • Public • Published

prevent-handlers-default

npm ci coverage deps

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.

Install

yarn add @hocs/prevent-handlers-default

Usage

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);

📺 Check out live demo.

/@hocs/prevent-handlers-default/

    Package Sidebar

    Install

    npm i @hocs/prevent-handlers-default

    Weekly Downloads

    9

    Version

    0.4.0

    License

    MIT

    Unpacked Size

    12.8 kB

    Total Files

    5

    Last publish

    Collaborators

    • hocs