When you want to do something in response to a signal changing, you can use the
creeateEffect primitive.
Effects come with the following benefits:
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.
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.
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.
Count: 0
Double Count: 0 is even