Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
76d734d
docs: document Signal class for v2
bowheart Jul 23, 2025
3b226d1
docs: document `injectSignal` injector for v2
bowheart Jul 23, 2025
25f5442
docs: document `ZeduxNode` class for v2
bowheart Jul 23, 2025
b85ec47
docs: document `AtomInstance` class for v2
bowheart Jul 23, 2025
dda386e
docs: document `Ecosystem` class for v2
bowheart Jul 24, 2025
7399aa8
docs: document `AtomTemplate` class for v2
bowheart Jul 24, 2025
8feebc5
docs: document glossary for v2
bowheart Jul 24, 2025
08ab6e6
docs: document `atom` factory for v2
bowheart Jul 24, 2025
15ab275
docs: document `AtomApi` class for v2
bowheart Jul 24, 2025
cd3e5c1
docs: document `api` factory for v2
bowheart Jul 24, 2025
98223ed
docs: document `SelectorInstance` class for v2
bowheart Jul 24, 2025
142c2f6
docs: document `AtomProvider` component for v2
bowheart Jul 24, 2025
66a18e6
docs: document `EcosystemProvider` component for v2
bowheart Jul 24, 2025
a653a5e
docs: document `ion` factory for v2
bowheart Jul 24, 2025
a4bc835
docs: document `MappedSignal` class for v2
bowheart Jul 24, 2025
040c9b2
docs: document `useEcosystem` hook for v2
bowheart Jul 24, 2025
81d257f
docs: document `createEcosystem` factory for v2
bowheart Jul 24, 2025
ecf9bd9
docs: document `injectEcosystem` injector for v2
bowheart Jul 24, 2025
2a4365c
docs: document `useAtomContext` hook for v2
bowheart Jul 24, 2025
4f03145
docs: document `useAtomInstance` hook for v2
bowheart Jul 24, 2025
09f5137
docs: document `useAtomSelector` hook for v2
bowheart Jul 25, 2025
f4797fc
docs: document `SelectorTemplate` type for v2
bowheart Jul 25, 2025
6a96082
docs: document `useAtomState` hook for v2
bowheart Jul 25, 2025
12d4322
docs: document `useAtomValue` hook for v2
bowheart Jul 25, 2025
f4b616f
docs: document `injectAtomInstance` for v2
bowheart Jul 25, 2025
df53ac7
docs: document `injectAtomState` injector for v2
bowheart Jul 25, 2025
95aaf09
docs: document `injectAtomValue` injector for v2
bowheart Jul 25, 2025
97b1ea9
docs: document `injectCallback` injector for v2
bowheart Jul 25, 2025
94d8f41
docs: document `injectEffect` injector for v2
bowheart Jul 31, 2025
e9e1309
docs: document `injectHydration` injector for v2
bowheart Aug 1, 2025
9b64c91
docs: document `injectMappedSignal` for v2
bowheart Aug 4, 2025
e261b14
docs: document `injectMemo` for v2
bowheart Aug 5, 2025
71368db
docs: document `injectPromise` for v2
bowheart Nov 7, 2025
6dbc2af
update /not-done injectPromise links
bowheart Nov 7, 2025
5b5d326
fix pnpm-lock zedux v1 peer deps
bowheart Nov 15, 2025
4ad17cc
docs: document `injectRef` for v2
bowheart Nov 15, 2025
0dd2766
update /not-done paths for `injectRef`
bowheart Nov 15, 2025
9bdb2c3
docs: document `injectSelf` for v2
bowheart Nov 15, 2025
efbe060
some tweaks
bowheart Nov 15, 2025
ec91a3a
docs: document `injectWhy` for v2
bowheart Nov 15, 2025
767885f
docs: document the `AtomConfig` type for v2
bowheart Nov 15, 2025
6f7583b
docs: document the new `inject` util
bowheart Nov 15, 2025
eca6cc0
docs: document the new `As` util
bowheart Nov 15, 2025
50a0921
docs: document the new `getDefaultEcosystem` util for v2
bowheart Nov 15, 2025
89cd2b2
docs: document `getInternals` for v2
bowheart Nov 15, 2025
3413f6a
docs: document `is` util for v2
bowheart Nov 16, 2025
2f720fc
docs: document `setInternals` for v2
bowheart Nov 16, 2025
6d61266
docs: document new `untrack` util
bowheart Nov 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: document useAtomContext hook for v2
  • Loading branch information
bowheart committed Jul 24, 2025
commit 2a4365c38efaf205a1a136412c6bcff0c0e98902
2 changes: 1 addition & 1 deletion docs/docs/v2/api/classes/AtomInstance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function Parent() {
}
```

Consume provided instances with [`useAtomContext()`](/not-done?path=../hooks/useAtomContext)
Consume provided instances with [`useAtomContext()`](../hooks/useAtomContext.mdx).

```ts
import { useAtomContext } from '@zedux/react'
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/v2/api/components/AtomProvider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AtomProvider } from '@zedux/react'

A component that provides one or more [atom instances](../classes/AtomInstance) over React context.

Use [`useAtomContext()`](/not-done?path=../hooks/useAtomContext) to consume the provided instance(s).
Use [`useAtomContext()`](../hooks/useAtomContext.mdx) to consume the provided instance(s).

## Example

Expand Down Expand Up @@ -159,5 +159,5 @@ AtomProvider accepts **either** an `instance` prop to provide a single atom inst
## See Also

- [The React Context walkthrough](../../../walkthrough/react-context.mdx)
- [The `useAtomContext` hook](/not-done?path=../hooks/useAtomContext)
- [The `useAtomContext` hook](../hooks/useAtomContext.mdx)
- [The `useAtomInstance` hook](/not-done?path=../hooks/useAtomInstance)
117 changes: 117 additions & 0 deletions docs/docs/v2/api/hooks/useAtomContext.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
id: useAtomContext
title: useAtomContext
---

import { Legend, Item, Link, Tabs, Ts, tab1, tab2 } from '@site/src/all'

```ts
import { useAtomContext } from '@zedux/react'
```

A React hook that retrieves an [atom instance](../classes/AtomInstance.mdx) provided over React context via an [`<AtomProvider>`](../components/AtomProvider.mdx).

This hook only returns the atom instance itself. To make the consuming component rerender when the provided atom updates, pass the returned atom instance to [`useAtomValue`](/not-done?path=./useAtomValue.mdx) or [`useAtomState`](/not-done?path=./useAtomState.mdx).

## Example

```tsx live ecosystemId=useAtomContext/example resultVar=App version=2
const themeAtom = atom('theme', (_id: number) => 'light')

function ThemedComponent() {
const themeInstance = useAtomContext(themeAtom)
const theme = useAtomValue(themeInstance)

return (
<div
style={{
background: theme === 'light' ? 'white' : 'black',
color: theme === 'light' ? 'black' : 'white',
padding: '20px',
}}
>
Current theme: {theme}
</div>
)
}

function ThemeToggle() {
const themeInstance = useAtomContext(themeAtom)
const [theme, setTheme] = useAtomState(themeInstance)

return (
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle Theme
</button>
)
}

function App() {
const instance1 = useAtomInstance(themeAtom, [1])
const instance2 = useAtomInstance(themeAtom, [2])

return (
<>
<AtomProvider instance={instance1}>
<div>
<ThemedComponent />
<ThemeToggle />
</div>
</AtomProvider>
<AtomProvider instance={instance2}>
<div>
<ThemedComponent />
<ThemeToggle />
</div>
</AtomProvider>
</>
)
}
```

## Signature

<Tabs>
{tab1(`useAtomContext = (template, defaultParams?) => atomInstance
// or
useAtomContext = (template, throwIfNotProvided?) => providedAtomInstance`)}
{tab2(`declare const useAtomContext: {
<A extends AnyAtomTemplate>(template: A): NodeOf<A> | undefined;
<A extends AnyAtomTemplate>(template: A, defaultParams: ParamsOf<A>): NodeOf<A>;
<A extends AnyAtomTemplate>(template: A, throwIfNotProvided: boolean): NodeOf<A>;
}`)}
</Tabs>

<Legend>
<Item name="template">
Required. The atom template that identifies which atom instance to retrieve from context.

</Item>
<Item name="defaultParams">
Optional. An array.

If no atom instance was provided for the given template, Zedux will use these params to create and return a new atom instance.

</Item>
<Item name="throwIfNotProvided">
Optional. A boolean. Whether to throw an error if no atom instance was provided.

This is the recommended overload in almost all situations:

```tsx
const providedInstance = useAtomContext(myTemplate, true)
```

</Item>
<Item name="Returns">
The provided [atom instance](../classes/AtomInstance.mdx), or the newly-created atom instance if no atom instance was provided and `defaultParams` were passed.

</Item>
</Legend>

## See Also

- [The `AtomProvider` component](../components/AtomProvider.mdx)
- [The `useAtomValue` hook](/not-done?path=./useAtomValue.mdx)
- [The React Context walkthrough](../../../walkthrough/react-context.mdx)
- [The `AtomInstance` class](../classes/AtomInstance.mdx)
2 changes: 1 addition & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ module.exports = {
{
type: 'category',
label: 'Hooks',
items: ['v2/api/hooks/useEcosystem'],
items: ['v2/api/hooks/useAtomContext', 'v2/api/hooks/useEcosystem'],
},
{
type: 'category',
Expand Down