Browse Source

Check if router is ready on page load (#241)

Thomas Brandého 2 years ago
parent
commit
f370642205
2 changed files with 5 additions and 1 deletions
  1. 3 0
      pynecone/compiler/templates.py
  2. 2 1
      pynecone/compiler/utils.py

+ 3 - 0
pynecone/compiler/templates.py

@@ -158,9 +158,12 @@ SOCKET = constants.SOCKET
 STATE = constants.STATE
 STATE = constants.STATE
 EVENTS = constants.EVENTS
 EVENTS = constants.EVENTS
 SET_RESULT = format_state_setter(RESULT)
 SET_RESULT = format_state_setter(RESULT)
+READY = f"const {{ isReady }} = {ROUTER};"
 USE_EFFECT = join(
 USE_EFFECT = join(
     [
     [
         "useEffect(() => {{",
         "useEffect(() => {{",
+        "  if(!isReady)",
+        f"    return;",
         f"  if (!{SOCKET}.current) {{{{",
         f"  if (!{SOCKET}.current) {{{{",
         f"    connect({SOCKET}, {{state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})",
         f"    connect({SOCKET}, {{state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})",
         "  }}",
         "  }}",

+ 2 - 1
pynecone/compiler/utils.py

@@ -119,7 +119,8 @@ def compile_state(state: Type[State]) -> str:
     )
     )
     router = templates.ROUTER
     router = templates.ROUTER
     socket = templates.SOCKET
     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:
 def compile_events(state: Type[State]) -> str: