Skip to content

Commit

Permalink
docs: add documentation for useProvideContext
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinecula committed Nov 4, 2022
1 parent 4069d4f commit 23b7541
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/docs/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Currently Haunted supports the following hooks:
- [useEffect](./useEffect/)
- [useLayoutEffect](./useLayoutEffect/)
- [useMemo](./useMemo/)
- [useProvideContext](./useProvideContext/)
- [useReducer](./useReducer/)
- [useRef](./useRef/)
- [useState](./useState/)
63 changes: 63 additions & 0 deletions docs/docs/hooks/useProvideContext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
layout: layout-api
package: haunted
module: lib/use-provide-context.js
---

# Hooks >> useProvideContext

Makes your component become a Context provider. It updates every nested consumer with the current value. You can optionally use a deps array (useful when the context value is mutated instead of being replaced).

```js playground use-provide-context use-provide-context.js
import {
html,
component,
useState,
useContext,
createContext,
useProvideContext,
} from "haunted";

const ThemeContext = createContext("dark");

customElements.define("theme-consumer", ThemeContext.Consumer);

function Consumer() {
const context = useContext(ThemeContext);
return context;
}

customElements.define("my-consumer", component(Consumer));

function App() {
const [theme, setTheme] = useState("light");
useProvideContext(ThemeContext, theme);

return html`
<select value=${theme} @change=${(event) => setTheme(event.target.value)}>
<option value="dark">Dark</option>
<option value="light">Light</option>
</select>
<my-consumer></my-consumer>
<theme-provider .value=${theme === "dark" ? "light" : "dark"}>
<theme-consumer
.render=${(value) =>
html`
<h1>${value}</h1>
`}
></theme-consumer>
</theme-provider>
`;
}

customElements.define("use-provide-context", component(App));
```

```html playground-file use-provide-context index.html
<script type="module" src="use-provide-context.js"></script>
<use-provide-context></use-provide-context>
```

## API
1 change: 1 addition & 0 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [useEffect](./hooks/useEffect/)
- [useLayoutEffect](./hooks/useLayoutEffect/)
- [useMemo](./hooks/useMemo/)
- [useProvideContext](./hooks/useProvideContext/)
- [useReducer](./hooks/useReducer/)
- [useRef](./hooks/useRef/)
- [useState](./hooks/useState/)
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Currently Haunted supports the following hooks:
- [useEffect](https://hauntedhooks.netlify.app/docs/hooks/useEffect/)
- [useLayoutEffect](https://hauntedhooks.netlify.app/docs/hooks/useLayoutEffect/)
- [useMemo](https://hauntedhooks.netlify.app/docs/hooks/useMemo/)
- [useProvideContext](https://hauntedhooks.netlify.app/docs/hooks/useProvideContext/)
- [useReducer](https://hauntedhooks.netlify.app/docs/hooks/useReducer/)
- [useRef](https://hauntedhooks.netlify.app/docs/hooks/useRef/)
- [useState](https://hauntedhooks.netlify.app/docs/hooks/useState/)
Expand Down

0 comments on commit 23b7541

Please sign in to comment.