Skip to content

docs(connect): add connected generic component typing example#279

Open
ncthuc2004 wants to merge 3 commits into
piotrwitek:masterfrom
ncthuc2004:issuehunt-55-connected-generic
Open

docs(connect): add connected generic component typing example#279
ncthuc2004 wants to merge 3 commits into
piotrwitek:masterfrom
ncthuc2004:issuehunt-55-connected-generic

Conversation

@ncthuc2004

@ncthuc2004 ncthuc2004 commented May 16, 2026

Copy link
Copy Markdown

Summary

  • add a dedicated connected generic component pattern using a small factory (createConnectedGenericList<T>())
  • show connect + ConnectedProps inference so Redux-injected props stay strongly typed without any
  • document creation at module scope to avoid remount/reconnect churn

Why

Issue #55 requests a practical, compile-safe connected generic component example. This update combines the guide's generic list style with idiomatic React-Redux typing.

Validation

  • documentation-only update
  • snippet follows existing MyTypes / connect patterns already used in the guide

IssueHunt Summary

Referenced issues

This pull request has been submitted to:


@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request adds documentation and examples for connecting generic components using a factory pattern and React-Redux's ConnectedProps. The review identifies a TypeScript type mismatch where the connector is defined with insufficient props for the generic component, suggesting the connector be moved inside the factory. There is also a recommendation to use unique item keys rather than array indices for selection logic to ensure more robust component behavior.

Comment thread README.md Outdated
Comment on lines +1841 to +1857
const connector = connect(mapStateToProps);
type ReduxProps = ConnectedProps<typeof connector>;

export const createConnectedGenericList = <T,>() => {
type Props = GenericListProps<T> & ReduxProps;

const GenericList: React.FC<Props> = ({ items, renderItem, getItemKey, selectedId }) => (
<ul>
{items.map((item, index) => (
<li key={getItemKey(item)} data-selected={String(index) === selectedId}>
{renderItem(item)}
</li>
))}
</ul>
);

return connector(GenericList);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The connector is defined using OwnProps, which only includes the selectedId property. When connector(GenericList) is called, TypeScript will throw an error because GenericList requires additional props (items, renderItem, getItemKey) that are not present in the OwnProps definition used to create the connector.

To fix this while maintaining type safety for generic components, you should define the connect call (or the connector) inside the factory where it can be aware of the full set of props, including the generic ones.

Comment thread README_SOURCE.md Outdated
Comment on lines +735 to +751
const connector = connect(mapStateToProps);
type ReduxProps = ConnectedProps<typeof connector>;

export const createConnectedGenericList = <T,>() => {
type Props = GenericListProps<T> & ReduxProps;

const GenericList: React.FC<Props> = ({ items, renderItem, getItemKey, selectedId }) => (
<ul>
{items.map((item, index) => (
<li key={getItemKey(item)} data-selected={String(index) === selectedId}>
{renderItem(item)}
</li>
))}
</ul>
);

return connector(GenericList);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The connector is typed using OwnProps, which only includes the selectedId property. When connector(GenericList) is called, TypeScript will throw an error because GenericList requires additional props (items, renderItem, getItemKey) that are not present in the OwnProps definition used to create the connector.

To fix this while maintaining type safety for generic components, you should define the connect call (or the connector) inside the factory where it can be aware of the full set of props, including the generic ones.

Comment thread README.md Outdated
const GenericList: React.FC<Props> = ({ items, renderItem, getItemKey, selectedId }) => (
<ul>
{items.map((item, index) => (
<li key={getItemKey(item)} data-selected={String(index) === selectedId}>

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 selection logic String(index) === selectedId relies on the array index. In a generic list component where a unique key is available via getItemKey(item), it is more idiomatic and robust to compare the item's key against the selectedId instead of the index.

Suggested change
<li key={getItemKey(item)} data-selected={String(index) === selectedId}>
<li key={getItemKey(item)} data-selected={getItemKey(item) === selectedId}>

Comment thread README_SOURCE.md Outdated
const GenericList: React.FC<Props> = ({ items, renderItem, getItemKey, selectedId }) => (
<ul>
{items.map((item, index) => (
<li key={getItemKey(item)} data-selected={String(index) === selectedId}>

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 selection logic String(index) === selectedId relies on the array index. In a generic list component where a unique key is available via getItemKey(item), it is more idiomatic and robust to compare the item's key against the selectedId instead of the index.

Suggested change
<li key={getItemKey(item)} data-selected={String(index) === selectedId}>
<li key={getItemKey(item)} data-selected={getItemKey(item) === selectedId}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant