Skip to content

ClientStateVar accepts invalid JavaScript identifiers and emits broken hook code #6828

Open

Description

@harsh21234i

Description

ClientStateVar.create() only validates that var_name is a string, but the value is later inserted directly into generated JavaScript identifiers.

For example, this is currently accepted by the Python API:

from reflex.experimental import ClientStateVar

is_open = ClientStateVar.create("is-open", default=False)


def index():
    return rx.text(is_open.value)

The generated hook name is derived directly from var_name:

setter_name = f"set{var_name.capitalize()}"
hooks = {
    f"const [{var_name}, {setter_name}] = useState({default_var!s})": None,
}

With var_name="is-open", that produces invalid JavaScript along the lines of:

const [is-open, setIs-open] = useState(false)

The same problem applies to names containing spaces, punctuation, or reserved JavaScript words.

Expected behavior

Reflex should not generate invalid JavaScript from an accepted ClientStateVar name.

Possible fixes:

  • Validate var_name as a safe JavaScript identifier and raise a clear ValueError for invalid names.
  • Or generate a safe internal hook identifier and keep the user-provided name only for stable ref keys.

Actual behavior

Invalid names are accepted by ClientStateVar.create(), then used directly in hook declarations and setter names, causing generated frontend code to fail.

Area

Frontend/client-state runtime.

Duplicate check

I searched existing issues and PRs for ClientStateVar with invalid identifiers, hyphenated names, client_state var_name, and generated setIs-open output, and did not find an existing report for this behavior.

Activity

  1. linear-code commented on Aug 1, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions