{"record":{"id":"d334a263cfebf09e","repo":"reflex-dev/reflex","slug":"the-condition-must-be-set","errorCode":null,"errorMessage":"The condition must be set.","messagePattern":"The condition must be set\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-components-core/src/reflex_components_core/core/cond.py","lineNumber":190,"sourceCode":"def cond(condition: Any, c1: Any, c2: Any = types.Unset(), /) -> Component | Var:\n    \"\"\"Create a conditional component or Prop.\n\n    Args:\n        condition: The cond to determine which component to render.\n        c1: The component or prop to render if the cond_var is true.\n        c2: The component or prop to render if the cond_var is false.\n\n    Returns:\n        The conditional component.\n\n    Raises:\n        ValueError: If the arguments are invalid.\n    \"\"\"\n    # Convert the condition to a Var.\n    cond_var = LiteralVar.create(condition)\n    if cond_var is None:\n        msg = \"The condition must be set.\"\n        raise ValueError(msg)\n\n    # If the first component is a component, create a Cond component.\n    if isinstance(c1, BaseComponent):\n        if not isinstance(c2, types.Unset) and not isinstance(c2, BaseComponent):\n            return Cond.create(cond_var.bool(), c1, Fragment.create(c2))\n        return Cond.create(cond_var.bool(), c1, c2)\n\n    # Otherwise, create a conditional Var.\n    # Check that the second argument is valid.\n    if isinstance(c2, BaseComponent):\n        return Cond.create(cond_var.bool(), Fragment.create(c1), c2)\n    if isinstance(c2, types.Unset):\n        msg = \"For conditional vars, the second argument must be set.\"\n        raise ValueError(msg)\n\n    # convert the truth and false cond parts into vars so the _var_data can be obtained.\n    c1_var = Var.create(c1)\n    c2_var = Var.create(c2)","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-components-core/src/reflex_components_core/core/cond.py#L172-L208","documentation":"`rx.cond(condition, c1, c2)` needs a condition that can be converted into a Var. `LiteralVar.create(condition)` returning None means the condition is None or of a type Reflex cannot render as a literal — so there is nothing to branch on.","triggerScenarios":"`rx.cond(None, a, b)`, `rx.cond(untyped_object, ...)`, or passing a value whose type LiteralVar.create does not handle.","commonSituations":"Conditionally building UI where the condition variable was never initialized; passing a Python object (dict/dataclass) instead of a bool, comparison, or Var; refactoring leaves the condition expression empty.","solutions":["Pass a real boolean expression or State Var: rx.cond(State.is_logged_in, ...)","Default None conditions: rx.cond(condition if condition is not None else False, ...)","Ensure the condition is a supported type (bool, int, str, Var, comparison result)"],"exampleFix":"// before\nrx.cond(State.user, greeting, login_form)  # user is None or unsupported type\n// after\nrx.cond(State.user is not None, greeting, login_form)","handlingStrategy":"type-guard","validationCode":"if condition is None:\n    condition = False\ncomp = rx.cond(condition, c1, c2)","typeGuard":"def is_condable(c) -> bool:\n    return c is not None and (isinstance(c, (bool, int, float, str, Var)) or hasattr(c, '_var_type') or callable(getattr(c, '__eq__', None)))","tryCatchPattern":null,"preventionTips":["Initialize state fields used as conditions with real defaults","Prefer explicit comparisons (State.x is not None) over raw object truthiness"],"tags":["reflex","conditional-rendering","validation"],"backgroundTag":"missing-condition-expression","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}