useClientEffect$() to execute code after the component is resumed. This is useful for setting up timers or streams on the client when the application is resumed.
useClientEffect$() receives a
track() function just like
useWatch$(). Use the
track() function to trigger the effect when the store is updated. See reactivity for more information.
Component Life Cycle and SSR
Qwik is resumable. Resumability means that the application starts on the server and then is transferred to the client. On the client, the application continues execution from where it left off. A common use case is creating a component on the server, pausing, and then resuming on the client. To make the component fully functional, it may be necessary to execute code eagerly on the client to set up timers or streams.
useClientEffect$() is a client only method. (There is no equivalent on the server.)
useWatch$()for behavior that needs to be executed on both client and server.
Client effect code is executed after the component is resumed. The
useClientEffect$() method takes an additional argument that controls when the effect is executed. There are two options:
visible(default): Execute the effect when the component becomes visible. This is a preferred option because it delays the execution of the effect until the component is visible rather than eagerly on application startup (We are trying to minimize the amount of code the application runs on startup).
load: Execute the code as soon as possible. This is usually right after
The example shows a clock component that is rendered below the fold. Use the
useClientEffect$() to make the clock update the current time every second to make it work on the client. We have provided the utility function
updateClock to aid your implementation.
Keep in mind that
useClientEffect$() should return a cleanup function that releases the
setInterval timer so that the timer can be properly cleaned up when the component is unmounted/destroyed.