From f37064220569516e5b198eb7f06ffe727a643de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Tue, 10 Jan 2023 21:22:05 +0100 Subject: [PATCH] Check if router is ready on page load (#241) --- pynecone/compiler/templates.py | 3 +++ pynecone/compiler/utils.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pynecone/compiler/templates.py b/pynecone/compiler/templates.py index 2fcefee12..79f567a33 100644 --- a/pynecone/compiler/templates.py +++ b/pynecone/compiler/templates.py @@ -158,9 +158,12 @@ SOCKET = constants.SOCKET STATE = constants.STATE EVENTS = constants.EVENTS SET_RESULT = format_state_setter(RESULT) +READY = f"const {{ isReady }} = {ROUTER};" USE_EFFECT = join( [ "useEffect(() => {{", + " if(!isReady)", + f" return;", f" if (!{SOCKET}.current) {{{{", f" connect({SOCKET}, {{state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})", " }}", diff --git a/pynecone/compiler/utils.py b/pynecone/compiler/utils.py index 0364a7ac2..3a2e84f02 100644 --- a/pynecone/compiler/utils.py +++ b/pynecone/compiler/utils.py @@ -119,7 +119,8 @@ def compile_state(state: Type[State]) -> str: ) router = templates.ROUTER socket = templates.SOCKET - return templates.join([synced_state, result, router, socket]) + ready = templates.READY + return templates.join([synced_state, result, router, socket, ready]) def compile_events(state: Type[State]) -> str: