def _render_stateful_code(
        cls,
        component: Component,
        tag_name: str,
    ) -> str:
        """Render the code for a stateful component.

        Args:
            component: The component to render.
            tag_name: The tag name for the stateful component (see _get_tag_name).

        Returns:
            The rendered code.
        """
        # Memoize event triggers useCallback to avoid unnecessary re-renders.
        memo_event_triggers = tuple(cls._get_memoized_event_triggers(component).items())

        # Trigger hooks stored separately to write after the normal hooks (see stateful_component.js.jinja2)
        memo_trigger_hooks = []

        if memo_event_triggers:
            # Copy the component to avoid mutating the original.
            component = copy.copy(component)

            for event_trigger, (
                memo_trigger,
                memo_trigger_hook,
            ) in memo_event_triggers:
                # Replace the event trigger with the memoized version.
                memo_trigger_hooks.append(memo_trigger_hook)
                component.event_triggers[event_trigger] = memo_trigger

        # Render the code for this component and hooks.
        return STATEFUL_COMPONENT.render(
            tag_name=tag_name,
            memo_trigger_hooks=memo_trigger_hooks,
            component=component,
        )
Render the code for a stateful component.

        Args:
            component: The component to render.
            tag_name: The tag name for the stateful component (see _get_tag_name).

        Returns:
            The rendered code.
        
reflex/components/component.py