function createStoreContext<TValue>(): object;
Defined in: packages/angular-store/src/createStoreContext.ts:48
Creates a typed Angular dependency-injection context for sharing a bundle of atoms and stores with a component subtree.
The returned provideStoreContext function accepts a factory that creates the
context value. Using a factory (rather than a static value) ensures each
component instance β and each SSR request β receives its own state, avoiding
cross-request pollution.
Consumers call injectStoreContext() inside an injection context (typically a
constructor or field initializer) to retrieve the contextual atoms and stores,
then compose them with existing hooks like injectSelector,
injectSelector, and injectAtom.
Type Parameters#
TValue#
TValue extends object
Returns#
object
injectStoreContext#
injectStoreContext: () => TValue;
Returns#
TValue
provideStoreContext#
provideStoreContext: (factory) => Provider;
Parameters#
factory#
() => TValue
Returns#
Provider
Example#
const { provideStoreContext, injectStoreContext } = createStoreContext<{
countAtom: Atom<number>
totalsStore: Store<{ count: number }>
}>()
// Parent component provides the context
@Component({
providers: [
provideStoreContext(() => ({
countAtom: createAtom(0),
totalsStore: new Store({ count: 0 }),
})),
],
template: `<child-cmp />`,
})
class ParentComponent {}
// Child component consumes the context
@Component({ template: `{{ count() }}` })
class ChildComponent {
private ctx = injectStoreContext()
count = injectSelector(this.ctx.countAtom)
}
Throws#
When injectStoreContext() is called without a matching
provideStoreContext() in a parent component's providers.