Skip to content

Commit f1fd746

Browse files
committed
add exception in use_effect if user provides an async hook function
1 parent e3f479c commit f1fd746

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/reactpy/core/hooks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ def use_effect(
146146
Returns:
147147
If not function is provided, a decorator. Otherwise ``None``.
148148
"""
149+
if asyncio.iscoroutinefunction(function):
150+
raise TypeError(
151+
"`use_effect` does not support async functions. "
152+
"Use `use_async_effect` instead."
153+
)
154+
149155
hook = HOOK_STACK.current_hook()
150156
dependencies = _try_to_infer_closure_values(function, dependencies)
151157
memoize = use_memo(dependencies=dependencies)

tests/test_core/test_hooks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,3 +1287,24 @@ def bad_cleanup():
12871287
await layout.render()
12881288
component_hook.latest.schedule_render()
12891289
await layout.render() # no error
1290+
1291+
1292+
def test_use_effect_exception_on_async_function():
1293+
@reactpy.component
1294+
def ComponentWithBadEffect():
1295+
@reactpy.hooks.use_effect
1296+
async def bad_effect():
1297+
pass
1298+
1299+
return reactpy.html.div()
1300+
1301+
with assert_reactpy_did_log(
1302+
match_error="does not support async functions",
1303+
error_type=TypeError,
1304+
):
1305+
1306+
async def run_test():
1307+
async with reactpy.Layout(ComponentWithBadEffect()) as layout:
1308+
await layout.render()
1309+
1310+
asyncio.run(run_test())

0 commit comments

Comments
 (0)