DocsBlog
  • 1.1.2

  • light

    dark

    system

    Switch mode
  • Cerberus

    Acheron

    Elysium

Get Started
Components
Data Grid
Signals
Styling
Theming

Get Started

OverviewReactivityData FetchingStores

Primitives

createSignalcreateQuerycreateMutationcreateComputedcreateEffectcreateStoreContextbatch

Hooks

useQueryuseMutationuseReaduseSignal

Components

ReactiveText

On this page

  • Edit this page on Github

Reading Primitive Signals

Learn how to read primivite signals in React components.

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

Usage

When you want to read a signal value within a component without having fine-grained reactivity.

Note

This is an alternative API to ReactiveText and should only be used in very specific use cases.

Reading State

The useRead hook was created to provide low-level access control for signals. By utilizing this hook, you are opting out of fine-grained reactivity and other performance enhancing benefits.

Warning

This API should be used sparingly and only for very specific use cases where you know what you are doing.

Note

We always recommend utilizing ReactiveText over useRead to provide better control over React rendering in addition to a cleaner DX.

API

useRead accepts the following options:

ParamsRequiredDescription
accessortrueThe signal Accessor value to subscribe to.

Return

useRead returns the latest value of the signal Accessor.

Copy
'use client'

import { HStack } from '@/styled-system/jsx'
import { Button, Text } from '@cerberus/react'
import { createSignal, ReactiveText, useRead } from '@cerberus/signals'

function createReadStore() {
  const [count, setCount] = createSignal<number>(0)
  const [readRenderCount, setReadRenderCount] = createSignal<number>(0)
  return {
    count,
    readRenderCount,
    increment: () => setCount(count() + 1),
    updateRenderCount: () => setReadRenderCount(readRenderCount() + 1),
  }
}

/**
 * This entire component will re-render when the count changes
 * because it uses useRead.
 *
 * However any ReactiveText child componens will **never** re-render.
 */
export function ReadDemo() {
  const store = createReadStore()
  const countVal = useRead(() => store.count())

  const increment = () => store.increment()

  store.updateRenderCount()

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

      <HStack gap="md" w="full">
        <Text>Render Count:</Text>
        <ReactiveText data={store.readRenderCount} />
      </HStack>
    </HStack>
  )
}

0

Render Count:

1