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 中，你可以通过赋值获拿到元素的引用，因为 JSX 会像这下面样创建 DOM 元素："
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-keyword",
          children: "const"
        }), " myDiv = ", jsxs(_components.span, {
          className: "xml",
          children: [jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), ">"]
          }), "My Element", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), ">"]
          })]
        }), ";\n"]
      })
    }), "\n", jsx(_components.p, {
      children: "但是，避免将元素拆分并将放在单个连续的 JSX 模板中是有好处的，因为这样可以让 Solid 更好地对元素创建进行优化。"
    }), "\n", jsxs(_components.p, {
      children: ["与之类似地，在 Solid 中你可以使用 ", jsx(_components.code, {
        children: "ref"
      }), " 属性获取元素的引用。对 Ref 进行赋值类似于上面的例子，赋值行为是在元素创建时，在 DOM 被追加前发生的。 只需声明一个变量，元素引用就会赋值给该变量。"]
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-keyword",
          children: "let"
        }), " myDiv;\n\n", jsxs(_components.span, {
          className: "xml",
          children: [jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), " ", jsx(_components.span, {
              className: "hljs-attr",
              children: "ref"
            }), "=", jsx(_components.span, {
              className: "hljs-string",
              children: "{myDiv}"
            }), ">"]
          }), "My Element", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), ">"]
          })]
        }), ";\n"]
      })
    }), "\n", jsx(_components.p, {
      children: "因此，让我们获取对 canvas 元素的引用并为其设置动画："
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: ["<canvas ref={canvas} width=", jsx(_components.span, {
          className: "hljs-string",
          children: "\"256\""
        }), " height=", jsx(_components.span, {
          className: "hljs-string",
          children: "\"256\""
        }), " />\n"]
      })
    }), "\n", jsx(_components.p, {
      children: "Refs 也可以采用回调函数的形式。这便于封装逻辑，尤其是当你不需要等到元素被追加时。"
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-js",
        children: ["<div ref={", jsxs(_components.span, {
          className: "hljs-function",
          children: [jsx(_components.span, {
            className: "hljs-params",
            children: "el"
          }), " =>"]
        }), " ", jsx(_components.span, {
          className: "hljs-comment",
          children: "/* 处理 el... */"
        }), "}>", jsx(_components.span, {
          className: "hljs-title class_",
          children: "My"
        }), " ", jsx(_components.span, {
          className: "hljs-title class_",
          children: "Element"
        }), "</div>\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-7c14b526.js.map
