Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add experimental namespace under rx._x #2951

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reflex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
]

_MAPPING = {
"reflex.experimental": ["_x"],
"reflex.admin": ["admin", "AdminDash"],
"reflex.app": ["app", "App", "UploadFile"],
"reflex.base": ["base", "Base"],
Expand Down
1 change: 1 addition & 0 deletions reflex/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from reflex.experimental import _x as _x
from reflex import admin as admin
from reflex.admin import AdminDash as AdminDash
from reflex import app as app
Expand Down
14 changes: 14 additions & 0 deletions reflex/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Namespace for experimental features."""

from types import SimpleNamespace

from ..utils.console import warn
from . import hooks as hooks

warn(
"`rx._x` contains experimental features and might be removed at any time in the future .",
)

_x = SimpleNamespace(
hooks=hooks,
)
75 changes: 75 additions & 0 deletions reflex/experimental/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Add standard Hooks wrapper for React."""

from reflex.utils.imports import ImportVar
from reflex.vars import Var, VarData


def _add_react_import(v: Var | None, tags: str | list):
if v is None:
return

if isinstance(tags, str):
tags = [tags]

v._var_data = VarData( # type: ignore
imports={"react": [ImportVar(tag=tag) for tag in tags]},
)


def const(name, value) -> Var | None:
"""Create a constant Var.

Args:
name: The name of the constant.
value: The value of the constant.

Returns:
The constant Var.
"""
return Var.create(f"const {name} = {value}")


def useCallback(func, deps) -> Var | None:
"""Create a useCallback hook with a function and dependencies.

Args:
func: The function to wrap.
deps: The dependencies of the function.

Returns:
The useCallback hook.
"""
if deps:
v = Var.create(f"useCallback({func}, {deps})")
else:
v = Var.create(f"useCallback({func})")
_add_react_import(v, "useCallback")
return v


def useContext(context) -> Var | None:
"""Create a useContext hook with a context.

Args:
context: The context to use.

Returns:
The useContext hook.
"""
v = Var.create(f"useContext({context})")
_add_react_import(v, "useContext")
return v


def useRef(default) -> Var | None:
"""Create a useRef hook with a default value.

Args:
default: The default value of the ref.

Returns:
The useRef hook.
"""
v = Var.create(f"useRef({default})")
_add_react_import(v, "useRef")
return v
Loading