@hocs/with-callback-on-change-while

0.3.0 • Public • Published

🔔 with-callback-on-change-while

npm ci coverage deps

Part of a collection of Higher-Order Components for React, especially useful with Recompose.

Invokes a callback on prop change while condition is true, useful to decouple side effects like onChange handler in a declarative way and control loops.

Install

yarn add @hocs/with-callback-on-change-while

Usage

withCallbackOnChangeWhile(
  propName: string,
  shouldCall: (props: Object) => boolean,
  callback: (props: Object) => void
): HigherOrderComponent
import React from 'react';
import { compose, withState, withHandlers } from 'recompose';
import withCallbackOnChangeWhile from '@hocs/with-callback-on-change-while';

const Demo = ({ count, onButtonClick }) => (
  <div>
    <h1>{count}</h1>
    <button onClick={onButtonClick}>increment</button>
  </div>
);

export default compose(
  withState('count', 'setCount', 0),
  withHandlers({
    onButtonClick: ({ setCount, count }) => () => setCount(count + 1)
  }),
  withCallbackOnChangeWhile(
    'count',
    ({ count }) => count <= 5,
    ({ count }) => console.log(count)
  )
)(Demo);

📺 Check out live demo.

Package Sidebar

Install

npm i @hocs/with-callback-on-change-while

Weekly Downloads

22

Version

0.3.0

License

MIT

Unpacked Size

11 kB

Total Files

5

Last publish

Collaborators

  • hocs