{"record":{"id":"9ce4346cef2a7c35","repo":"reflex-dev/reflex","slug":"the-render-function-must-return-a-component","errorCode":null,"errorMessage":"The render function must return a component.","messagePattern":"The render function must return a component\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/components/tags/iter_tag.py","lineNumber":111,"sourceCode":"        if len(args) == 1:\n            # If the render function doesn't take the index as an argument.\n            component = self.render_fn(arg)\n        else:\n            # If the render function takes the index as an argument.\n            if len(args) != 2:\n                msg = \"The render function must take 2 arguments.\"\n                raise ValueError(msg)\n            component = self.render_fn(arg, index)\n\n        # Nested foreach components or cond must be wrapped in fragments.\n        if isinstance(component, (Foreach, Cond)):\n            component = Fragment.create(component)\n\n        component = _into_component_once(component)\n\n        if component is None:\n            msg = \"The render function must return a component.\"\n            raise ValueError(msg)\n\n        # Set the component key.\n        if component.key is None:\n            component.key = index\n\n        return component\n","sourceCodeStart":93,"sourceCodeEnd":118,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/components/tags/iter_tag.py#L93-L118","documentation":"The render function passed to `rx.foreach` (IterTag) returned something that is not a component after normalization — `None`, a raw string/number, or a value `_into_component_once` cannot convert.","triggerScenarios":"`rx.foreach(State.items, lambda item: item.name)` (returns a str instead of `rx.text(item.name)`), or a render fn with a branch returning None.","commonSituations":"Assuming foreach auto-wraps values like `rx.text` does, or early-return logic inside the render function that forgets a component on one path.","solutions":["Wrap returned values in a component: `rx.text(item.name)`","Ensure every branch of the render function returns a component (use `rx.fragment()` as an empty fallback)"],"exampleFix":"# before\nrx.foreach(State.items, lambda item: item.name)\n\n# after\nrx.foreach(State.items, lambda item: rx.text(item.name))","handlingStrategy":"type-guard","validationCode":"import reflex as rx\n\ndef returns_component(fn, sample_item) -> bool:\n    return isinstance(fn(sample_item), rx.Component)","typeGuard":"import reflex as rx\n\ndef is_component_returning_fn(fn) -> bool:\n    import typing\n    ret = typing.get_type_hints(fn).get(\"return\", None)\n    return isinstance(ret, type) and issubclass(ret, rx.Component)","tryCatchPattern":null,"preventionTips":["Always wrap values: rx.text(item) not item","Annotate render fns `-> rx.Component` so type checkers catch bad returns"],"tags":["reflex","foreach","render-function","return-type"],"backgroundTag":"callback-return-type-invalid","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}