Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
- [- HOC wrapping a component](#--hoc-wrapping-a-component)
- [- HOC wrapping a component and injecting props](#--hoc-wrapping-a-component-and-injecting-props)
- [- Nested HOC - wrapping a component, injecting props and connecting to redux 🌟](#--nested-hoc---wrapping-a-component-injecting-props-and-connecting-to-redux-)
- [- Legacy Recompose examples](#--legacy-recompose-examples)
- [Redux Connected Components](#redux-connected-components)
- [- Redux connected counter](#--redux-connected-counter)
- [- Redux connected counter with own props](#--redux-connected-counter-with-own-props)
Expand Down Expand Up @@ -974,6 +975,174 @@ export default () => (

[⇧ back to top](#table-of-contents)

### - Legacy Recompose examples

[`recompose`](https://github.com/acdlite/recompose) is no longer actively maintained, so prefer [Hooks](#hooks) for new code.
These examples are intended for maintaining or migrating existing HOC-based codebases that already depend on `recompose`.

The examples keep consumer props separate from injected props while using `compose`, `withState`, `withHandlers`, `withProps`, and `withStateHandlers`.
Only `@types/recompose` is added to the playground so these source-backed documentation examples stay type-checked without adding the legacy runtime package to the app bundle.

```tsx
import * as React from 'react';
import { compose, withHandlers, withProps, withState, withStateHandlers } from 'recompose';

type CounterOuterProps = {
title: string;
initialCount?: number;
step?: number;
};

type CounterStateProps = {
count: number;
setCount: (nextCount: number | ((currentCount: number) => number)) => void;
};

type CounterHandlers = {
onIncrement: () => void;
onReset: () => void;
};

type CounterLabelProps = {
label: string;
};

type CounterProps = CounterOuterProps &
CounterStateProps &
CounterHandlers &
CounterLabelProps;

const CounterView: React.FC<CounterProps> = ({
count,
label,
onIncrement,
onReset,
}) => (
<section>
<h3>{label}</h3>
<p>Current count: {count}</p>
<button type="button" onClick={onIncrement}>
Increment
</button>
<button type="button" onClick={onReset}>
Reset
</button>
</section>
);

const withCounterState = withState<
CounterOuterProps,
number,
'count',
'setCount'
>(
'count',
'setCount',
({ initialCount = 0 }) => initialCount
);

const withCounterHandlers = withHandlers<
CounterOuterProps & CounterStateProps,
CounterHandlers
>({
onIncrement:
({ setCount, step = 1 }) =>
() => {
setCount(count => count + step);
},
onReset:
({ initialCount = 0, setCount }) =>
() => {
setCount(initialCount);
},
});

const withCounterLabel = withProps<
CounterLabelProps,
CounterOuterProps & CounterStateProps & CounterHandlers
>(({ count, title }) => ({
label: `${title}: ${count}`,
}));

const enhanceCounter = compose<CounterProps, CounterOuterProps>(
withCounterState,
withCounterHandlers,
withCounterLabel
);

export const RecomposeCounter = enhanceCounter(CounterView);

type ToggleOuterProps = {
label: string;
initiallyOpen?: boolean;
};

type ToggleState = {
isOpen: boolean;
};

type ToggleUpdaters = {
toggle: () => ToggleState;
close: () => ToggleState;
};

type ToggleProps = ToggleOuterProps & ToggleState & ToggleUpdaters;

const ToggleView: React.FC<ToggleProps> = ({ close, isOpen, label, toggle }) => (
<section>
<button type="button" onClick={toggle}>
{label}
</button>
{isOpen && (
<button type="button" onClick={close}>
Close
</button>
)}
</section>
);

const withToggleState = withStateHandlers<
ToggleState,
ToggleUpdaters,
ToggleOuterProps
>(
({ initiallyOpen = false }) => ({
isOpen: initiallyOpen,
}),
{
toggle:
({ isOpen }) =>
() => ({
isOpen: !isOpen,
}),
close: () => () => ({
isOpen: false,
}),
}
);

export const RecomposeToggle = withToggleState(ToggleView);

```
<details><summary><i>Click to expand</i></summary><p>

```tsx
import * as React from 'react';

import { RecomposeCounter, RecomposeToggle } from './recompose-examples';

export default () => (
<>
<RecomposeCounter title="Recompose counter" initialCount={3} step={2} />
<RecomposeToggle label="Show details" initiallyOpen />
</>
);

```
</p></details>

[⇧ back to top](#table-of-contents)

---

## Redux Connected Components
Expand Down
14 changes: 14 additions & 0 deletions README_SOURCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
- [- HOC wrapping a component](#--hoc-wrapping-a-component)
- [- HOC wrapping a component and injecting props](#--hoc-wrapping-a-component-and-injecting-props)
- [- Nested HOC - wrapping a component, injecting props and connecting to redux 🌟](#--nested-hoc---wrapping-a-component-injecting-props-and-connecting-to-redux-)
- [- Legacy Recompose examples](#--legacy-recompose-examples)
- [Redux Connected Components](#redux-connected-components)
- [- Redux connected counter](#--redux-connected-counter)
- [- Redux connected counter with own props](#--redux-connected-counter-with-own-props)
Expand Down Expand Up @@ -438,6 +439,19 @@ Adds error handling using componentDidCatch to any component

[⇧ back to top](#table-of-contents)

### - Legacy Recompose examples

[`recompose`](https://github.com/acdlite/recompose) is no longer actively maintained, so prefer [Hooks](#hooks) for new code.
These examples are intended for maintaining or migrating existing HOC-based codebases that already depend on `recompose`.

The examples keep consumer props separate from injected props while using `compose`, `withState`, `withHandlers`, `withProps`, and `withStateHandlers`.
Only `@types/recompose` is added to the playground so these source-backed documentation examples stay type-checked without adding the legacy runtime package to the app bundle.

::codeblock='playground/src/hoc/recompose-examples.tsx'::
::expander='playground/src/hoc/recompose-examples.usage.tsx'::

[⇧ back to top](#table-of-contents)

---

## Redux Connected Components
Expand Down
20 changes: 20 additions & 0 deletions playground/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/react-dom": "18.0.3",
"@types/react-redux": "7.1.24",
"@types/react-router-dom": "5.3.3",
"@types/recompose": "0.30.15",
"axios": "0.26.1",
"cuid": "2.1.8",
"react": "18.1.0",
Expand Down
138 changes: 138 additions & 0 deletions playground/src/hoc/recompose-examples.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import * as React from 'react';
import { compose, withHandlers, withProps, withState, withStateHandlers } from 'recompose';

type CounterOuterProps = {
title: string;
initialCount?: number;
step?: number;
};

type CounterStateProps = {
count: number;
setCount: (nextCount: number | ((currentCount: number) => number)) => void;
};

type CounterHandlers = {
onIncrement: () => void;
onReset: () => void;
};

type CounterLabelProps = {
label: string;
};

type CounterProps = CounterOuterProps &
CounterStateProps &
CounterHandlers &
CounterLabelProps;

const CounterView: React.FC<CounterProps> = ({
count,
label,
onIncrement,
onReset,
}) => (
<section>
<h3>{label}</h3>
<p>Current count: {count}</p>
<button type="button" onClick={onIncrement}>
Increment
</button>
<button type="button" onClick={onReset}>
Reset
</button>
</section>
);

const withCounterState = withState<
CounterOuterProps,
number,
'count',
'setCount'
>(
'count',
'setCount',
({ initialCount = 0 }) => initialCount
);

const withCounterHandlers = withHandlers<
CounterOuterProps & CounterStateProps,
CounterHandlers
>({
onIncrement:
({ setCount, step = 1 }) =>
() => {
setCount(count => count + step);
},
onReset:
({ initialCount = 0, setCount }) =>
() => {
setCount(initialCount);
},
});

const withCounterLabel = withProps<
CounterLabelProps,
CounterOuterProps & CounterStateProps & CounterHandlers
>(({ count, title }) => ({
label: `${title}: ${count}`,
}));

const enhanceCounter = compose<CounterProps, CounterOuterProps>(
withCounterState,
withCounterHandlers,
withCounterLabel
);

export const RecomposeCounter = enhanceCounter(CounterView);

type ToggleOuterProps = {
label: string;
initiallyOpen?: boolean;
};

type ToggleState = {
isOpen: boolean;
};

type ToggleUpdaters = {
toggle: () => ToggleState;
close: () => ToggleState;
};
Comment on lines +98 to +101

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In recompose, the state updaters returned by withStateHandlers do not return the new state to the caller at runtime; they trigger an internal setState and return void (or undefined).

Typing them as returning ToggleState creates a type mismatch (a "type lie") where the compiler believes a value is returned, but at runtime it will be undefined.

To ensure type safety and accurately reflect runtime behavior, these handlers should be typed as returning void.

Suggested change
type ToggleUpdaters = {
toggle: () => ToggleState;
close: () => ToggleState;
};
type ToggleUpdaters = {
toggle: () => void;
close: () => void;
};


type ToggleProps = ToggleOuterProps & ToggleState & ToggleUpdaters;

const ToggleView: React.FC<ToggleProps> = ({ close, isOpen, label, toggle }) => (
<section>
<button type="button" onClick={toggle}>
{label}
</button>
{isOpen && (
<button type="button" onClick={close}>
Close
</button>
)}
</section>
);

const withToggleState = withStateHandlers<
ToggleState,
ToggleUpdaters,
ToggleOuterProps
>(
({ initiallyOpen = false }) => ({
isOpen: initiallyOpen,
}),
{
toggle:
({ isOpen }) =>
() => ({
isOpen: !isOpen,
}),
close: () => () => ({
isOpen: false,
}),
}
);

export const RecomposeToggle = withToggleState(ToggleView);
Loading