babel-plugin-jsx-merge-props

0.0.2 • Public • Published

babel-plugin-jsx-merge-props

Build Status npm size

Why?

It's annoying to use props that provided from multiple hooks.

const useFirstHook = () => {
  ...
 
  return {
    className,
    style,
    onClick,
  };
};
 
const useSecondHook = () => {
  ...
 
  return {
    value,
    style,
    onClick,
  };
};
const MyComponent = () => {
  const first = useFirstHook(...);
  const second = useSecondHook(...);
 
  const handleClick = useCallback(event => {
    first.onClick(event);
    second.onClick(event);
  }, [first, second]);
 
  return (
    <input
      type="text"
      {...first}
      {...second}
      className={[first.className, second.className].join(' ')}
      style={{ ...first.style, ...second.style }}
      onClick={handleClick}
    />
  );
}

Or would be better to use some package like merge-props, but It's still little bit bothersome.

import mergeProps from 'merge-props';
 
const MyComponent = () => {
  const first = useFirstHook(...);
  const second = useSecondHook(...);
 
  return (
    <input
      type="text"
      {...mergeProps(first, second)}
    />
  );
}

You can just write like below with this plugin.

import mergeProps from 'merge-props';
 
const MyComponent = () => {
  const first = useFirstHook(...);
  const second = useSecondHook(...);
 
  return (
    <input merge={mergeProps}
      type="text"
      {...first}
      {...second}
    />
  );
}

Installation

npm install --save-dev babel-plugin-jsx-merge-props

Usage

.babelrc:

{
  "plugins": [ "babel-plugin-jsx-merge-props" ]
}

Package Sidebar

Install

npm i babel-plugin-jsx-merge-props

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

5 kB

Total Files

3

Last publish

Collaborators

  • hooriza