0.1.0 • Published 1 month ago

react-wrap-with v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

react-wrap-with

Wrap a React component in another component by Higher-Order Component.

Background

When using React Context, sometimes it is inevitable to create an intermediate component to consume the context.

This function will wrap as an intermediate component and reduce complexity in the code.

How to use

import { createRoot } from 'react-render';
import { wrapWith } from 'react-wrap-with';

const Blink = ({ children, effect }) => <span className={`effect effect--${effect}`}>{children}</span>;
const withBlink = wrapWith(Blink, { effect: 'blink' });

const Hello = withBlink(() => <h1>Hello</h1>);

createRoot(document.getElementById('root')).render(<Hello />);

Will produce the HTML:

<span class="effect effect--blink">
  <h1>Hello</h1>
</span>

API

function wrapWith(
  wrapperComponentType: ComponentType<WrapperProps>,
  props: WrapperProps
): (wrappedComponentType: ComponentType<WrappedProps>) => ComponentType<WrappedProps>;

Behaviors

TypeScript: All wrappers must have props of children

Wrappers must allow children props. This is because wrapper is going to wrap around another component in parent-child relationship.

If you are seeing the following error in TypeScript, please make sure the wrapper component allow children props. React.PropsWithChildren<> is a typing helper to add children to any props.

Argument of type 'FC<Props>' is not assignable to parameter of type 'false | ComponentType<PropsWithChildren<EmptyProps>> | null | undefined'.
  Type 'FunctionComponent<Props>' is not assignable to type 'FunctionComponent<PropsWithChildren<EmptyProps>>'.
    Types of property 'propTypes' are incompatible.
      ...

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.

0.1.0

1 month ago

0.0.5-main.d38820a

5 months ago

0.0.5-main.d0b9a53

5 months ago

0.0.5-main.20e74fb

7 months ago

0.0.3-main.2182ccf

7 months ago

0.0.3-main.c42e1f0

7 months ago

0.0.3-main.01249e7

7 months ago

0.0.4-main.9b9491c

7 months ago

0.0.3-main.a78c9e9

7 months ago

0.0.3-main.b4ed649

7 months ago

0.0.5-main.d4a5bfa

7 months ago

0.0.3-main.849ef6f

7 months ago

0.0.3-main.c2c25a9

7 months ago

0.0.5-main.ccc612c

7 months ago

0.0.5-main.1db3426

7 months ago

0.0.5-main.c0aaee5

7 months ago

0.0.4-main.50f729d

7 months ago

0.0.3-main.c2f2663

7 months ago

0.0.3

7 months ago

0.0.4

7 months ago

0.0.3-main.c992a50

7 months ago

0.0.3-main.ce1e01f

8 months ago

0.0.3-main.63f9ac3

8 months ago

0.0.3-main.88b038e

8 months ago

0.0.3-main.29e9400

8 months ago

0.0.3-main.be9d7d2

8 months ago

0.0.3-main.fa45337

8 months ago

0.0.3-main.0d9d213

10 months ago

0.0.3-main.9ce5e01

8 months ago

0.0.3-main.668e736

8 months ago

0.0.3-main.9052b89

8 months ago

0.0.3-main.2b1d307

10 months ago

0.0.2

1 year ago

0.0.1

1 year ago