import { jsx, jsxs, Fragment } from 'solid-jsx/jsx-runtime';

/*@jsxRuntime automatic @jsxImportSource solid-jsx*/
const toc = [];
function _createMdxContent(props) {
  const _components = Object.assign({
    p: "p",
    pre: "pre",
    code: "code",
    span: "span"
  }, props.components);
  return jsxs(Fragment, {
    children: [jsx(_components.p, {
      children: "Solid 如此高性能的部分原因是 Solid 的组件基本上只是函数调用。我们通过编译器将潜在的响应式表达式包装在对象 getter 中来传播更新。可以想象编译器输出："
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-comment",
          children: "// 输入"
        }), "\n<", jsx(_components.span, {
          className: "hljs-title class_",
          children: "MyComp"
        }), " dynamic={", jsx(_components.span, {
          className: "hljs-title function_",
          children: "mySignal"
        }), "()}>\n  ", jsx(_components.span, {
          className: "xml",
          children: jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "Child"
            }), " />"]
          })
        }), "\n</", jsx(_components.span, {
          className: "hljs-title class_",
          children: "MyComp"
        }), ">\n\n", jsx(_components.span, {
          className: "hljs-comment",
          children: "// 输出"
        }), "\n", jsx(_components.span, {
          className: "hljs-title class_",
          children: "MyComp"
        }), "({\n  ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "get"
        }), " ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "dynamic"
        }), "() { ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "return"
        }), " ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "mySignal"
        }), "() },\n  ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "get"
        }), " ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "children"
        }), "() { ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "return"
        }), " ", jsx(_components.span, {
          className: "hljs-title class_",
          children: "Child"
        }), "() }\n});\n"]
      })
    }), "\n", jsx(_components.p, {
      children: "这意味着这些 props 会被惰性求值。props 的访问将被推迟到某些地方有用到它们。这保留了响应性，而不会引入无关的封装代码或同步行为。然而，这意味着存在子组件或元素的情况下，重复访问可能会导致重新创建。"
    }), "\n", jsx(_components.p, {
      children: "大多数情况下，你只是将这些入参属性插入到 JSX 中，所以不会有问题。但是由于 children 元素可能会被重复创建，所以当你处理 children 时需要格外小心。"
    }), "\n", jsxs(_components.p, {
      children: ["出于这个原因，Solid 提供了 ", jsx(_components.code, {
        children: "children"
      }), " 工具函数。此方法既会根据 children 访问创建 memo 缓存，还会处理任何嵌套的子级响应式引用，以便可以直接与 children 交互。"]
    }), "\n", jsxs(_components.p, {
      children: ["在示例中，我们有一个动态列表，我们希望设置它们的 ", jsx(_components.code, {
        children: "color"
      }), " 样式属性。如果我们直接与 ", jsx(_components.code, {
        children: "props.children"
      }), " 交互，不仅会多次创建节点，还会发现 children 本身是一个从 ", jsx(_components.code, {
        children: "<For>"
      }), " 返回的 Memo 函数。"]
    }), "\n", jsxs(_components.p, {
      children: ["让我们在 ", jsx(_components.code, {
        children: "colored-list.tsx"
      }), " 中使用 ", jsx(_components.code, {
        children: "children"
      }), " 工具函数："]
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-keyword",
          children: "export"
        }), " ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "default"
        }), " ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "function"
        }), " ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "ColoredList"
        }), "(", jsx(_components.span, {
          className: "hljs-params",
          children: "props"
        }), ") {\n  ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "const"
        }), " c = ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "children"
        }), "(", jsx(_components.span, {
          className: "hljs-function",
          children: "() =>"
        }), " props.", jsx(_components.span, {
          className: "hljs-property",
          children: "children"
        }), ");\n  ", jsx(_components.span, {
          className: "hljs-keyword",
          children: "return"
        }), " ", jsxs(_components.span, {
          className: "xml",
          children: [jsx(_components.span, {
            className: "hljs-tag",
            children: "<>"
          }), "{c()}", jsx(_components.span, {
            className: "hljs-tag",
            children: "</>"
          })]
        }), "\n}\n"]
      })
    }), "\n", jsx(_components.p, {
      children: "现在让我们创建一个 Effect 来更新元素。"
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-title function_",
          children: "createEffect"
        }), "(", jsx(_components.span, {
          className: "hljs-function",
          children: "() =>"
        }), " ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "c"
        }), "().", jsx(_components.span, {
          className: "hljs-title function_",
          children: "forEach"
        }), "(", jsxs(_components.span, {
          className: "hljs-function",
          children: [jsx(_components.span, {
            className: "hljs-params",
            children: "item"
          }), " =>"]
        }), " item.", jsx(_components.span, {
          className: "hljs-property",
          children: "style"
        }), ".", jsx(_components.span, {
          className: "hljs-property",
          children: "color"
        }), " = props.", jsx(_components.span, {
          className: "hljs-property",
          children: "color"
        }), "));\n"]
      })
    })]
  });
}
function MDXContent(props = {}) {
  const {wrapper: MDXLayout} = props.components || ({});
  return MDXLayout ? jsx(MDXLayout, Object.assign({}, props, {
    children: jsx(_createMdxContent, props)
  })) : _createMdxContent(props);
}

export { MDXContent as default, toc };
//# sourceMappingURL=lesson-d9b34b15.js.map
