Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Component Rendering with Memoization #15

Open
lazarevtill opened this issue Apr 6, 2025 · 0 comments
Open

Optimize Component Rendering with Memoization #15

lazarevtill opened this issue Apr 6, 2025 · 0 comments

Comments

@lazarevtill
Copy link
Contributor

Optimize Component Rendering with Memoization

Problem:
Several components re-render unnecessarily, especially in the chat interface.

Recommendation:
Apply memo, useCallback, and useMemo more consistently across components.

// Example optimization for app/components/chat-input/suggestions.tsx
const MemoizedSuggestion = memo(function Suggestion({ suggestion, onClick }) {
  return (
    <PromptSuggestion onClick={() => onClick(suggestion)}>
      {suggestion}
    </PromptSuggestion>
  );
});

// Update parent component
export const Suggestions = memo(function Suggestions({
  onValueChange,
  onSuggestion,
  value,
}: SuggestionsProps) {
  // Existing code...
  
  const handleSuggestionClick = useCallback(
    (suggestion: string) => {
      setActiveCategory(null);
      onSuggestion(suggestion);
      onValueChange("");
    },
    [onSuggestion, onValueChange]
  );
  
  // Updated return with memoized child components
  return (
    <AnimatePresence mode="popLayout">
      {showCategorySuggestions ? (
        <motion.div>
          {activeCategoryData?.items.map((suggestion: string, index: number) => (
            <MemoizedSuggestion 
              key={`${suggestion}-${index}`}
              suggestion={suggestion}
              onClick={handleSuggestionClick}
            />
          ))}
        </motion.div>
      ) : (
        // Rest of the component...
      )}
    </AnimatePresence>
  );
});

Impact: Medium/Low - Reduces unnecessary re-renders

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

No branches or pull requests

1 participant