DocsBlog

Get Started

Components

Data Grid

Signals

Styling

Theming

↑↓Navigate
↵Select
EscClose
  • 1.8.0

  • Day

    Night

    Preview

    Switch mode
  • cerberus

    acheron

    elysium

    oceanus

Get Started
Components
Data Grid
Signals
Styling
Theming

Get started

OverviewReactivityData FetchingGlobal Stores

Primitives

Creating SignalsCreating QueriesCreating MutationsComputing Signal ValuesCreating EffectsContextual Signal StoresBatch UpdatesCleanup EffectsUntrack Signals

Hooks

Using QueriesUsing MutationsReading Primitive SignalsUsing SignalsUsing Store Instances

Components

Reactive Text

Creating Effects

Learn how to create effects using Signals in React.

  • source
View as Markdown
Open this page in Markdown
Anthropic
Open in Claude
Ask questions about this page
OpenAI
Open in ChatGPT
Ask questions about this page
import { createEffect } from '@cerberus/signals'

Usage

When you want to do something in response to a signal changing, you can use the creeateEffect primitive.

Effects come with the following benefits:

  1. Can be used anywhere in your application (e.g., not constrained to components)
  2. Can be nested within other effects
  3. Can be used within native React effects
  4. Auto-tracks signals (e.g., no dependency Array needed)

Basically, as long as the scope has access to a signal Accessor the usage is valid.

In this example, we utilize the createEffect to set the isEven signal value when getCount is updated.

Note

In order for createEffect to track signal subscriptions, it is required that Accessors are used in the body.

Loading example...

With React effects

You can nest createEffect within the native React effect when you need to access the React component render lifecycle.

Note

The createEffect primitive removes the overall need for native React effects.

The only time you should use native React effects is if you need to access the React component lifecycle. All other scenarios can be achieved using any of the primitive APIs this library offers.

Loading example...

API

createEffect accepts a callback Function and returns a Function. When the return Function is called, it will unsubscribe the effect from the signal Observer.

Important: be sure to use Accessor getters not values within a createEffect primitive. Ignoring to do so will cause stale values that will not be updated.

On this page

  • Usage
  • With React effects
  • API
Edit this page on Github

Count: 0

Double Count: 0 is even