DocsBlog
  • 1.3.0

  • light

    dark

    system

    Switch mode
  • Cerberus

    Acheron

    Elysium

Get Started
Components
Data Grid
Signals
Styling
Theming

Get Started

OverviewReactivityData FetchingStores

Primitives

createSignalcreateQueryNewcreateMutationNewcreateComputedcreateEffectcreateStoreContextbatchonCleanupNewuntrackNew

Hooks

useQueryNewuseMutationuseReaduseSignaluseStoreNew

Components

ReactiveText

On this page

Loading...

Loading...

Loading...

Loading...

Using Store Instances

Learn how to use instances of stores as an alternative to Context.

  • source
import { useStore } from '@cerberus/signals'

Usage

When you want to use a unqiue instance of a global Store but do not need a Context API.

Using the Store

Simply pass in a store to the useStore hook and recieve a memoized version of it - protected against re-renders.


For more information about stores, see the Stores page.

API

useStore accepts the following options:

ParamsRequiredDescription
storeYesThe store function reference to call.

Return

useStore returns a memoized version of the store value (e.g., what the store returns);

Copy
'use client'

import { HStack, Stack } from '@/styled-system/jsx'
import { Button, Text } from '@cerberus/react'
import { ReactiveText, useStore } from '@cerberus/signals'
import { useEffect } from 'react'
import { createRenderStore } from '../render-store'

export function UseStoreDemo() {
  const store = useStore(createRenderStore)

  const increment = () => {
    store.setCount((prev) => prev + 1)
  }

  useEffect(() => {
    return () => store.onUnmount()
  }, [store])

  store.trackRenders()

  return (
    <HStack justify="space-between" w="3/4">
      <Button onClick={increment}>Increment</Button>

      <Stack>
        <Text>
          Count: <ReactiveText data={store.count} />
        </Text>
        <Text>
          Render Count: <ReactiveText data={store.renderCount} />
        </Text>
      </Stack>
    </HStack>
  )
}

Count: 0

Render Count: 1

On this page

  • Usage
  • Using the Store
  • API
  • Return
  • Edit this page on Github