Use useClientEffect$() to execute code after the component is resumed. This is useful for things such as setting up timers or streams on the client when the application is resumed.
Component Life Cycle and SSR
Qwik is resumable. Resumability means that the application starts up on the server and then the application is transferred to the client. On the client, the application continues execution from where it left off. A common use case of this is that a component is created on the server, paused, and then resumed 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.) NOTE: See useWatch$() for behavior that needs to be executed on both client and server.
When is useClientEffect$() executed?
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 afterDOMContentLoadedevent.
Example
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.