Skip to content

Conversation

@GulSam00
Copy link

close #857

Description

This PR addresses a Hydration Mismatch Error that occurs when the SplitText component is nested inside a parent <p> tag (e.g., <p cannot be a descendant of <p>).

Previously, the component relied on a hardcoded switch statement that defaulted to rendering a <p> tag, making it impossible to satisfy HTML nesting rules even when a user wanted to render it as a span.

Changes

I have refactored the renderTag function to support Dynamic Tag Rendering (Polymorphic Component Pattern).

  1. Removed the verbose switch statement: Replaced it with a direct assignment (const Tag = tag;).
  2. Type Safety: Since the tag prop is typed as a strict Union Type ('h1' | ... | 'span' | 'p'), there is no risk of invalid or unsafe HTML tags being rendered.
  3. Default Behavior: It continues to default to 'p' (via default props), preserving backward compatibility.

Code Comparison

Before (Rigid Switch Case):

switch (tag) {
  case 'h1': return <h1 ...>{text}</h1>;
  // ... repeated code ...
  default: return <p ...>{text}</p>;
}

After (Dynamic & Type-Safe):

const renderTag = () => {
  // ... styles ...
  
  // The 'tag' prop is already type-checked (Union Type), so it's safe to use directly.
  const Tag = tag; 

  return (
    <Tag ref={ref} style={style} className={classes}>
      {text}
    </Tag>
  );
};

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.

[BUG]: Resolve hydration error caused by nested p tags

1 participant