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"
  }, props.components);
  return jsxs(Fragment, {
    children: [jsx(_components.p, {
      children: "Solid 提供了一个 Context API 来传递数据，Context 不依赖于 props 传递。这对于共享 Signal 和 Store 很有用。使用 Context 的好处是创建的 Context 是响应式系统的一部分创建并由之管理。"
    }), "\n", jsxs(_components.p, {
      children: ["首先，我们创建一个 Context 对象。该对象包含一个用于注入数据的 ", jsx(_components.code, {
        children: "Provider"
      }), " 组件。但是通常的做法是将 ", jsx(_components.code, {
        children: "Provider"
      }), " 组件和 ", jsx(_components.code, {
        children: "useContext"
      }), " 消费者与已经为 Context 设置的数据封装在一起。"]
    }), "\n", jsxs(_components.p, {
      children: ["这正是我们在本教程中的内容。你可以在 ", jsx(_components.code, {
        children: "counter.tsx"
      }), " 文件中查看简单计数器 Store 的定义。"]
    }), "\n", jsxs(_components.p, {
      children: ["要使用 Context，我们需要使用 ", jsx(_components.code, {
        children: "CounterProvider"
      }), " 组件包装 App 组件以在全局范围内注入 Context。并在设置初始计数值为 1。"]
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-title function_",
          children: "render"
        }), "(", jsx(_components.span, {
          className: "hljs-function",
          children: "() =>"
        }), " (\n  ", jsxs(_components.span, {
          className: "xml",
          children: [jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "CounterProvider"
            }), " ", jsx(_components.span, {
              className: "hljs-attr",
              children: "count"
            }), "=", jsx(_components.span, {
              className: "hljs-string",
              children: "{1}"
            }), ">"]
          }), "\n    ", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "App"
            }), " />"]
          }), "\n  ", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "CounterProvider"
            }), ">"]
          })]
        }), "\n), ", jsx(_components.span, {
          className: "hljs-variable language_",
          children: "document"
        }), ".", jsx(_components.span, {
          className: "hljs-title function_",
          children: "getElementById"
        }), "(", jsx(_components.span, {
          className: "hljs-string",
          children: "\"app\""
        }), "));\n"]
      })
    }), "\n", jsxs(_components.p, {
      children: ["接下来我们需要在 ", jsx(_components.code, {
        children: "nested.tsx"
      }), " 组件中消费数据。可以通过使用封装的 ", jsx(_components.code, {
        children: "useCounter"
      }), " 消费者来达到目的。"]
    }), "\n", jsx(_components.pre, {
      children: jsxs(_components.code, {
        className: "hljs language-jsx",
        children: [jsx(_components.span, {
          className: "hljs-keyword",
          children: "const"
        }), " [count, { increment, decrement }] = ", jsx(_components.span, {
          className: "hljs-title function_",
          children: "useCounter"
        }), "();\n", jsx(_components.span, {
          className: "hljs-keyword",
          children: "return"
        }), " (\n  ", jsxs(_components.span, {
          className: "xml",
          children: [jsx(_components.span, {
            className: "hljs-tag",
            children: "<>"
          }), "\n    ", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), ">"]
          }), "{count()}", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "div"
            }), ">"]
          }), "\n    ", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "button"
            }), " ", jsx(_components.span, {
              className: "hljs-attr",
              children: "onClick"
            }), "=", jsx(_components.span, {
              className: "hljs-string",
              children: "{increment}"
            }), ">"]
          }), "+", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "button"
            }), ">"]
          }), "\n    ", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["<", jsx(_components.span, {
              className: "hljs-name",
              children: "button"
            }), " ", jsx(_components.span, {
              className: "hljs-attr",
              children: "onClick"
            }), "=", jsx(_components.span, {
              className: "hljs-string",
              children: "{decrement}"
            }), ">"]
          }), "-", jsxs(_components.span, {
            className: "hljs-tag",
            children: ["</", jsx(_components.span, {
              className: "hljs-name",
              children: "button"
            }), ">"]
          }), "\n  ", jsx(_components.span, {
            className: "hljs-tag",
            children: "</>"
          })]
        }), "\n);\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-a2c68161.js.map
