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: