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
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
- [Redux Connected Components](#redux-connected-components)
- [- Redux connected counter](#--redux-connected-counter)
- [- Redux connected counter with own props](#--redux-connected-counter-with-own-props)
- [- Redux connected counter with factory props](#--redux-connected-counter-with-factory-props)
- [- Redux connected counter via hooks](#--redux-connected-counter-via-hooks)
- [- Redux connected counter with `redux-thunk` integration](#--redux-connected-counter-with-redux-thunk-integration)
- [Context](#context)
Expand Down Expand Up @@ -1063,6 +1064,84 @@ export default () => (

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

### - Redux connected counter with factory props

```tsx
import Types from 'MyTypes';
import { connect, MapStateToPropsFactory } from 'react-redux';

import { countersActions, countersSelectors } from '../features/counters';
import { FCCounter } from '../components';

type OwnProps = {
label: string;
initialCount?: number;
};

type StateProps = {
count: number;
};

const makeMapStateToProps: MapStateToPropsFactory<
StateProps,
OwnProps,
Types.RootState
> = () => {
let previousCount: number | undefined;
let previousInitialCount: number | undefined;
let previousResult: StateProps | undefined;

return (state: Types.RootState, ownProps: OwnProps) => {
const count = countersSelectors.getReduxCounter(state.counters);
const initialCount = ownProps.initialCount || 0;

if (
previousResult &&
previousCount === count &&
previousInitialCount === initialCount
) {
return previousResult;
}

previousCount = count;
previousInitialCount = initialCount;
previousResult = {
count: count + initialCount,
};

return previousResult;
};
};

const dispatchProps = {
onIncrement: countersActions.increment,
};

export const FCCounterConnectedFactory = connect(
makeMapStateToProps,
dispatchProps
)(FCCounter);

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

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

import { FCCounterConnectedFactory } from './fc-counter-connected-factory';

export default (
<div>
<FCCounterConnectedFactory label="Counter A" initialCount={10} />
<FCCounterConnectedFactory label="Counter B" initialCount={20} />
</div>
);

```
</p></details>

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

### - Redux connected counter via hooks

```tsx
Expand Down
8 changes: 8 additions & 0 deletions README_SOURCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
- [Redux Connected Components](#redux-connected-components)
- [- Redux connected counter](#--redux-connected-counter)
- [- Redux connected counter with own props](#--redux-connected-counter-with-own-props)
- [- Redux connected counter with factory props](#--redux-connected-counter-with-factory-props)
- [- Redux connected counter via hooks](#--redux-connected-counter-via-hooks)
- [- Redux connected counter with `redux-thunk` integration](#--redux-connected-counter-with-redux-thunk-integration)
- [Context](#context)
Expand Down Expand Up @@ -456,6 +457,13 @@ Adds error handling using componentDidCatch to any component

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

### - Redux connected counter with factory props

::codeblock='playground/src/connected/fc-counter-connected-factory.tsx'::
::expander='playground/src/connected/fc-counter-connected-factory.usage.tsx'::

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

### - Redux connected counter via hooks

::codeblock='playground/src/hooks/react-redux-hooks.tsx'::
Expand Down
54 changes: 54 additions & 0 deletions playground/src/connected/fc-counter-connected-factory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Types from 'MyTypes';
import { connect, MapStateToPropsFactory } from 'react-redux';

import { countersActions, countersSelectors } from '../features/counters';
import { FCCounter } from '../components';

type OwnProps = {
label: string;
initialCount?: number;
};
Comment on lines +7 to +10

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

The OwnProps type is missing the label property, which is required by the FCCounter component. Since label is passed to the connected component in the usage example but not provided by mapStateToProps or mapDispatchToProps, it must be included in OwnProps to ensure type safety and avoid a compilation error when calling connect.

Suggested change
type OwnProps = {
initialCount?: number;
};
type OwnProps = {
label: string;
initialCount?: number;
};


type StateProps = {
count: number;
};

const makeMapStateToProps: MapStateToPropsFactory<
StateProps,
OwnProps,
Types.RootState
> = () => {
let previousCount: number | undefined;
let previousInitialCount: number | undefined;
let previousResult: StateProps | undefined;

return (state: Types.RootState, ownProps: OwnProps) => {
const count = countersSelectors.getReduxCounter(state.counters);
const initialCount = ownProps.initialCount || 0;

if (
previousResult &&
previousCount === count &&
previousInitialCount === initialCount
) {
return previousResult;
}

previousCount = count;
previousInitialCount = initialCount;
previousResult = {
count: count + initialCount,
};

return previousResult;
};
};

const dispatchProps = {
onIncrement: countersActions.increment,
};

export const FCCounterConnectedFactory = connect(
makeMapStateToProps,
dispatchProps
)(FCCounter);
10 changes: 10 additions & 0 deletions playground/src/connected/fc-counter-connected-factory.usage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from 'react';

import { FCCounterConnectedFactory } from './fc-counter-connected-factory';

export default (
<div>
<FCCounterConnectedFactory label="Counter A" initialCount={10} />
<FCCounterConnectedFactory label="Counter B" initialCount={20} />
</div>
);
1 change: 1 addition & 0 deletions playground/src/connected/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './fc-counter-connected-bind-action-creators';
export * from './fc-counter-connected-factory';
export * from './fc-counter-connected-own-props';
export * from './fc-counter-connected';