Skip to content

🐛 Bug: Random key in CodeBlock (@copilotkit/react-ui) causes flickering during streaming #2669

Description

@RowanLane

Description

While using @copilotkit/react-ui@1.10.6 we noticed that code blocks in chat messages flicker whenever:

  • the chat viewport is scrolled (React re-renders the list), or
  • the assistant streams additional markdown into an existing message.

Root cause: the CodeBlock component rendered by Markdown sets key={Math.random()} on the underlying syntax highlighter. React therefore treats every update as a brand-new subtree, unmounting and remounting the code block on each render. The syntax highlighter starts over, producing a noticeable color flash.

Expected behaviour

Code blocks should preserve their DOM/state across re-renders so that syntax highlighting remains stable during scrolling and streaming updates.

Actual behaviour

Because the key changes on every render, React remounts the code block. Users see the highlighted code briefly disappear/reflash anytime the message list re-renders (e.g. when new tokens arrive).

Suggested fix

Replace the random key with a stable value derived from the block’s content (e.g. a hash of the language + value). That keeps React from remounting the subtree while still allowing different snippets to coexist.

Environment

  • @copilotkit/react-ui v1.10.6
  • React 18 / Next.js 14 (app router)

Thanks for all the work on CopilotKit! A stable key would immediately fix the UX regression without requiring downstream overrides.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions