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",
    code: "code",
    pre: "pre",
    span: "span",
    blockquote: "blockquote"
  }, props.components);
  return jsxs(Fragment, {
    children: [jsx(_components.p, {
      children: "Signal 是最核心的响应式基本要素（primitive）。Signal 包含随时间变化的值。"
    }), "\n", jsxs(_components.p, {
      children: ["要创建 Signal，你可以从 ", jsx(_components.code, {
        children: "solid-js"
      }), " 导入 ", jsx(_components.code, {
        children: "createSignal"
      }), "，并在 Counter 组件调用它，如下所示："]
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-keyword",
          children: "const"
        }), " [count, setCount] = ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "createSignal"
        }), "(", jsx(_components.span, {
          className: "hljs-number",
          children: "0"
        }), ");\n"]
      })
    }), "\n", jsxs(_components.p, {
      children: ["传递给 createSignal 调用的参数是初始值，createSignal 返回一个带有 2 个函数的数组，一个是 getter，一个是 setter。我们可以使用解构随意命名这些参数。根据场景，我们将 getter 命名为 ", jsx(_components.code, {
        children: "count"
      }), " 和 setter 命名为 ", jsx(_components.code, {
        children: "setCount"
      }), "。"]
    }), "\n", jsx(_components.p, {
      children: "需要注意的是，第一个返回值是一个 getter 而不是值本身。这是因为框架需要拦截读取值的任何位置以自动跟踪更改。所以务必记住，访问值的位置很重要。"
    }), "\n", jsxs(_components.p, {
      children: ["在本课中，我们将使用 ", jsx(_components.code, {
        children: "setInterval"
      }), " 来创建一个递增计数器。我们可以通过将下面的代码添加到我们的 Counter 组件来实现每一秒更新一次 ", jsx(_components.code, {
        children: "count"
      }), " Signal。"]
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-built_in",
          children: "setInterval"
        }), "(", jsx(_components.span, {
          className: "hljs-function",
          children: "() =>"
        }), " ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "setCount"
        }), "(", jsx(_components.span, {
          className: "hljs-title function_",
          children: "count"
        }), "() + ", jsx(_components.span, {
          className: "hljs-number",
          children: "1"
        }), "), ", jsx(_components.span, {
          className: "hljs-number",
          children: "1000"
        }), ");\n"]
      })
    }), "\n", jsx(_components.p, {
      children: "读取之前的计数，加 1，然后设置新值。"
    }), "\n", jsxs(_components.blockquote, {
      children: ["\n", jsx(_components.p, {
        children: "Solid 的 Signal 也可以接收一个函数，你可以在其中使用前一个值来设置下一个值。"
      }), "\n", jsx(_components.pre, {
        children: jsxs(_components.code, {
          className: "hljs language-jsx",
          children: [jsx(_components.span, {
            className: "hljs-title function_",
            children: "setCount"
          }), "(", jsxs(_components.span, {
            className: "hljs-function",
            children: ["(", jsx(_components.span, {
              className: "hljs-params",
              children: "c"
            }), ") =>"]
          }), " c + ", jsx(_components.span, {
            className: "hljs-number",
            children: "1"
          }), ");\n"]
        })
      }), "\n"]
    }), "\n", jsx(_components.p, {
      children: "最后，我们需要在 JSX 代码中读取 Signal。"
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-keyword",
          children: "return"
        }), " ", jsxs(_components.span, {
          className: "xml",
          children: [jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), ">"]
          }), "Count: {count()}", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "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-1616a5ad.js.map
