-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add documentation for useProvideContext
- Loading branch information
1 parent
4069d4f
commit 23b7541
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters