Fix: suggestion: connected generic component#232
Fix: suggestion: connected generic component#232obsofficer-ctrl wants to merge 7 commits intopiotrwitek:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new example to the playground, illustrating a robust pattern for integrating generic React components with Redux in a TypeScript environment. It specifically addresses the challenge of type inference when using Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request introduces a generic React list component (GenericList) and demonstrates its integration with Redux using a concrete TodoList subclass for Todo items, along with an example usage. Review feedback highlights several areas for improvement: addressing the anti-pattern of using array index as React keys by delegating <li> rendering and key assignment to the itemRenderer, updating the Redux store creation to use the non-deprecated configureStore from @reduxjs/toolkit, and refactoring to avoid duplication of the StoreState type definition between the component and usage files.
| return ( | ||
| <ul> | ||
| {items.map((item, idx) => ( | ||
| <li key={idx}>{itemRenderer(item)}</li> | ||
| ))} | ||
| </ul> | ||
| ); |
There was a problem hiding this comment.
Using an item's index as a key is an anti-pattern in React, especially for lists that can be reordered or have items added/removed. It can lead to performance issues and bugs with component state. A better practice is to use a unique and stable identifier from the data itself.
To make the GenericList component more robust and adhere to best practices, let's delegate the responsibility of rendering the list item, including its key, to the itemRenderer. This makes the GenericList a simpler wrapper and ensures the consumer, who knows the data structure, provides a correct key.
I'll suggest a corresponding change in generic-list-connected.usage.tsx.
return (
<ul>
{items.map(itemRenderer)}
</ul>
);
| @@ -0,0 +1,39 @@ | |||
| import * as React from 'react'; | |||
| import { createStore } from 'redux'; | |||
There was a problem hiding this comment.
| const todoItemRenderer = (item: Todo): JSX.Element => ( | ||
| <span> | ||
| [{item.completed ? 'x' : ' '}] {item.title} | ||
| </span> | ||
| ); |
There was a problem hiding this comment.
Following the suggestion to improve GenericList, this todoItemRenderer should now be responsible for rendering the full list item (<li>) and providing a unique key. Since Todo items have a unique id, we can use that for the key.
| const todoItemRenderer = (item: Todo): JSX.Element => ( | |
| <span> | |
| [{item.completed ? 'x' : ' '}] {item.title} | |
| </span> | |
| ); | |
| const todoItemRenderer = (item: Todo): JSX.Element => ( | |
| <li key={item.id}> | |
| [{item.completed ? 'x' : ' '}] {item.title} | |
| </li> | |
| ); |
| readonly completed: boolean; | ||
| }; | ||
|
|
||
| type StoreState = { |
| import { ConnectedTodoList, Todo } from './generic-list-connected'; | ||
|
|
||
| /** Store Setup */ | ||
| type StoreState = { | ||
| readonly todos: Todo[]; | ||
| }; |
There was a problem hiding this comment.
The StoreState type is duplicated from generic-list-connected.tsx. To improve maintainability, you should export it from generic-list-connected.tsx (I've left a separate comment for that) and import it here, removing the local definition.
| import { ConnectedTodoList, Todo } from './generic-list-connected'; | |
| /** Store Setup */ | |
| type StoreState = { | |
| readonly todos: Todo[]; | |
| }; | |
| import { ConnectedTodoList, Todo, StoreState } from './generic-list-connected'; | |
| /** Store Setup */ |
Summary
This PR addresses: #55
Task: suggestion: connected generic component
Platform: github
Changes
Looking at this issue, I need to create an example of a connected generic component that combines the patterns from
generic-listandsfc-counter-connectedexamples in the playground.Let me analyze the existing patterns in the repository to understand the code style, then create the necessary files.
Based on the repository structure, I need to:
Let me create the implementation:
Wait, let me look more carefully at the existing playground files to understand the exact patterns used. Let me create proper files following the exact style: