Skip to content

How to inject a tree into another tree without getting hydration errors? #2210

Answered by wooorm
hahnbeelee asked this question in Q&A
Discussion options

You must be logged in to vote

First, you’re using TypeScript but casting everything as any? That removes all the benefits from TypeScript. Refactored as pseudo code (btw, important: also see https://mdxjs.com/packages/remark-mdx/#types).

import type { Root } from 'mdast'
import visit from 'unist-util-visit'

const withReplaceSnippets = (snippetTreeMap: Record<string, Root>) => {
  return (tree: Root) => {
    visit(tree, (node) => {
      if (node.type === 'mdxJsxFlowElement' && node.name === 'Snippet') {
          const file = node.attributes.find((attr) => attr.name === 'file')
          if (file && typeof file.value === 'string') {
            const desiredSnippetTree = snippetTreeMap[file.value]
            node.name

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@hahnbeelee
Comment options

@wooorm
Comment options

@hahnbeelee
Comment options

@wooorm
Comment options

@wooorm
Comment options

Answer selected by hahnbeelee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants